一,Promise实例,只要有then里面有return,就可以无线then

特别注释:resolve 和 then里面的return  都可以返回Promise实例,如果promise实例触发了reject,都能走到最外层的reject

function test() {
return new Promise(resolve => {
resolve(test2())
})
} function test2() {
return new Promise((resolve, reject) => {
if (Math.random() * 10 > 5) {
resolve([])
} else {
reject(null)
}
})
} test()
.then(res => {
console.log('success')
console.log(res)
return 1
})
.then(res => {
console.log(res)
return 2
})
.then(res => {
console.log(res)
return 3
})
.then(res => {
console.log(res)
})
.catch(res => {
console.log('error')
console.log(res)
})

在then里面return promise

function test() {
return new Promise(resolve => {
resolve()
})
} function test2() {
return new Promise((resolve, reject) => {
if (Math.random() * 10 > 5) {
resolve([])
} else {
reject(null)
}
})
} test()
.then(res => {
return test2()
})
.then(res => {
console.log(res)
})
.catch(res => {
console.log('error')
console.log(res)
})

在resolve返回promise实例

function test() {
return new Promise(resolve => {
resolve(test2())
})
} function test2() {
return new Promise((resolve, reject) => {
if (Math.random() * 10 > 5) {
resolve([])
} else {
reject(null)
}
})
} test()
.then(res => {
console.log(res)
})
.catch(res => {
console.log('error')
console.log(res)
})

Promise.all正确使用,console.log(num)打印的是一个多维数组,多维数组的长度,等于promise.all(Array)里面Array的长度

    Promise.all([axios(), axios()]).then((num) => {
console.log(num)
})

promise使用的正确方式的更多相关文章

  1. jquery中取消和绑定hover事件的正确方式

    在网页设计中,我们经常使用jquery去响应鼠标的hover事件,和mouseover和mouseout事件有相同的效果,但是这其中其中如何使用bind去绑定hover方法呢?如何用unbind取消绑 ...

  2. 在iOS微信浏览器中自动播放HTML5 audio(音乐)的2种正确方式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. eclipse 导入包含子maven项目的maven项目时的正确方式(父子项目)

    eclipse 导入包含子maven项目的maven项目时的正确方式(父子项目) NO1 导入时依次选择 import > Maven > Existing Maven Projects ...

  4. Springboot 打jar包分离lib,配置文件正确方式(二)

    Springboot 打jar包分离lib,配置文件正确方式(二) 背景 从<Springboot 打jar包分离lib,配置文件正确方式>中,可以达到把配置文件和依赖第三方的jar包分离 ...

  5. 在EntityFramework6中管理DbContext的正确方式——2DbContext的默认行为(外文翻译)

    (译者注:使用EF开发应用程序的一个难点就在于对其DbContext的生命周期管理,你的管理策略是否能很好的支持上层服务 使用独立事务,使用嵌套事务,并行执行,异步执行等需求? Mehdi El Gu ...

  6. .NET Core中使用RabbitMQ正确方式

    .NET Core中使用RabbitMQ正确方式 首先甩官网:http://www.rabbitmq.com/ 然后是.NET Client链接:http://www.rabbitmq.com/dot ...

  7. [翻译]小提示:使用figure和figcaption元素的正确方式

    figure和figcaption是一对经常被一起使用的语义化标签.如果你还没有看过规范中的定义,现在有机会在你的项目中使用它们了.如果你不知道怎么用,下面是关于如何正确使用它们的一些提示. figu ...

  8. 2019-11-25-加强版在国内分发-UWP-应用正确方式-通过win32安装UWP应用

    原文:2019-11-25-加强版在国内分发-UWP-应用正确方式-通过win32安装UWP应用 title author date CreateTime categories 加强版在国内分发 UW ...

  9. 打开ElasticSearch、kibana、logstash的正确方式

    作者:玩世不恭的Coder时间:2020-03-08说明:原创不易,本文为原创文章,未经允许不可转载,转载前请联系作者 打开ElasticSearch.kibana.logstash的正确方式 前言一 ...

随机推荐

  1. 1.分布式配置中心 spring-cloud-config

    pring Cloud 版本:2.1.0.RELEASE 一.server端 1.maven依赖 <dependency> <groupId>org.springframewo ...

  2. WebLogic服务器

    WebLogic是美国Oracle公司出品的一个application server确切的说是一个基于JAVAEE架构的中间件,BEA WebLogic是用于开发.集成.部署和管理大型分布式Web应用 ...

  3. JVM 内存溢出(转载~)

    对于JVM的内存写过的文章已经有点多了,而且有点烂了,不过说那么多大多数在解决OOM的情况,于此,本文就只阐述这个内容,携带一些分析和理解和部分扩展内容,也就是JVM宕机中的一些问题,OK,下面说下O ...

  4. Swift开源parser

    https://www.prowidesoftware.com/products/core https://github.com/prowide/prowide-core-examples/blob/ ...

  5. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-7.接口配置文件自动映射到属性和实体类配置

    笔记 7.接口配置文件自动映射到属性和实体类配置     简介:使用@value注解配置文件自动映射到属性和实体类 1.添加 @Component或者Configuration 注解:        ...

  6. Qt编写自定义控件17-按钮进度条

    前言 按钮进度条,顾名思义,表面上长得像一个按钮,单击以后切换成进度条指示按钮单击动作执行的进度,主要用在一些需要直接在按钮执行动作显示对应进度的场景,在很多网页中经常看到这种效果,这个效果有个优点就 ...

  7. ubuntu---ssh连接

    17.04版本或其他版本: 1.第一步是.(这个必须先安装) apt-get install openssh-server 2.第二步: 当在这段输入ifconfig提示安装.再最后安装这个. 终端会 ...

  8. monkey 查找闪退页面的方法

    使用了命令 adb shell monkey  --pct-touch 100 -v -p  com.iBer.iBerAppV2  5000 >/Users/kaibinliu/Desktop ...

  9. matlab中如何给一个矩阵中的某几个特定位置赋值

    用sub2ind >> a=zeros(5); i = [2;3;4]; j = [1;4;2]; >> a(sub2ind(size(a), i, j))=1 a = 0 0 ...

  10. The working copy is locked due to a previous error.

    SVN报错: The working copy is locked due to a previous error. 不能更新项目代码... 解决方式: 解决:右键你的左侧管理目录中的相关目录,然后点 ...