一,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. DNA Sorting

    DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 105159   Accepted: 42124 De ...

  2. 使用KerasNet

    1.安装Python3.6,必须是3.6因为当前KerasNet的配套版本是3.6 https://www.python.org/ftp/python/3.6.8/python-3.6.8-amd64 ...

  3. LC 583. Delete Operation for Two Strings

    Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...

  4. js对象和jQuery对象的区别

    (1)js对象的三种基本定位方式 (A)通过ID属性:document.getElementById() (B)通过NAME属性:document.getElementsByName() (C)通过标 ...

  5. weight权重的属性

    权重是把屏幕剩余空间按比例分配 控件使用0dp,则实际的宽度比就等于权重比 控件wrap_content,那么权重越大,位置占的越多,再小不过wrap_content 控件match_parent,那 ...

  6. nohup 不废话应用

    后台运行 nohup ./hello > myout.file 2>&1 & 本界面可以通过 jobs -l 查看后台 top 也能找到PID号 杀进程 kill PID

  7. 【转载】execute、executeUpdate、executeQuery三者的区别(及返回值)

    1. ResultSet executeQuery(String sql); 执行SQL查询,并返回ResultSet 对象. 2.int executeUpdate(String sql); 可执行 ...

  8. flask(1.1)装饰器装饰多个视图函数出现的问题

    目录 1.装饰器装饰多个视图函数出现的问题 2.使用装饰器修复技术解决该问题 1.装饰器装饰多个视图函数出现的问题 代码实例: from flask import Flask, request, re ...

  9. uni-app 封装 http promise请求,仅提供 post,all,spread 方法

    简单封装一下 uni-app 的请求,因为项目中只用 post 请求,所以只封装了 post 和 all 方法. 更新,新增 spread 方法 2019-11-22 10:37:21 global. ...

  10. webdriver的八种定位

    转自https://zhuanlan.zhihu.com/p/54588889 在UI层面的自动化测试开发中,元素的定位与操作是基础,也是经常遇到的困难所在.webdriver提供了8种定位: 1. ...