promise使用的正确方式
一,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使用的正确方式的更多相关文章
- jquery中取消和绑定hover事件的正确方式
在网页设计中,我们经常使用jquery去响应鼠标的hover事件,和mouseover和mouseout事件有相同的效果,但是这其中其中如何使用bind去绑定hover方法呢?如何用unbind取消绑 ...
- 在iOS微信浏览器中自动播放HTML5 audio(音乐)的2种正确方式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- eclipse 导入包含子maven项目的maven项目时的正确方式(父子项目)
eclipse 导入包含子maven项目的maven项目时的正确方式(父子项目) NO1 导入时依次选择 import > Maven > Existing Maven Projects ...
- Springboot 打jar包分离lib,配置文件正确方式(二)
Springboot 打jar包分离lib,配置文件正确方式(二) 背景 从<Springboot 打jar包分离lib,配置文件正确方式>中,可以达到把配置文件和依赖第三方的jar包分离 ...
- 在EntityFramework6中管理DbContext的正确方式——2DbContext的默认行为(外文翻译)
(译者注:使用EF开发应用程序的一个难点就在于对其DbContext的生命周期管理,你的管理策略是否能很好的支持上层服务 使用独立事务,使用嵌套事务,并行执行,异步执行等需求? Mehdi El Gu ...
- .NET Core中使用RabbitMQ正确方式
.NET Core中使用RabbitMQ正确方式 首先甩官网:http://www.rabbitmq.com/ 然后是.NET Client链接:http://www.rabbitmq.com/dot ...
- [翻译]小提示:使用figure和figcaption元素的正确方式
figure和figcaption是一对经常被一起使用的语义化标签.如果你还没有看过规范中的定义,现在有机会在你的项目中使用它们了.如果你不知道怎么用,下面是关于如何正确使用它们的一些提示. figu ...
- 2019-11-25-加强版在国内分发-UWP-应用正确方式-通过win32安装UWP应用
原文:2019-11-25-加强版在国内分发-UWP-应用正确方式-通过win32安装UWP应用 title author date CreateTime categories 加强版在国内分发 UW ...
- 打开ElasticSearch、kibana、logstash的正确方式
作者:玩世不恭的Coder时间:2020-03-08说明:原创不易,本文为原创文章,未经允许不可转载,转载前请联系作者 打开ElasticSearch.kibana.logstash的正确方式 前言一 ...
随机推荐
- qt5之网络通信
QT5 TCP网络通讯 服务器与客户端建立连接listen() - connectToHost(); 触发newPendingConnect信号 实时数据通讯write(); read(); 触发 ...
- matlab遍历文件夹下所有图片和遍历所有子文件夹下图片
做图像处理实验,经常需要遍历当前文件下所有图片.matlab当然很早就考虑了这个问题,库函数dir就是完成这个工作的.函数返回的是一个存放所有目录下文件信息的结构体,通过遍历结构体就可以达到访问所有文 ...
- 2.jdk1.8+springboot中http1.1之tcp连接复用实现
接上篇:https://www.cnblogs.com/Hleaves/p/11284316.html 环境:jdk1.8 + springboot 2.1.1.RELEASE + feign-hys ...
- SpringCloud(三)之Feign实现负载均衡的使用
一 点睛 Feign是Netflix开发的声明式.模板化的HTTP客户端, Feign可以帮助我们更快捷.优雅地调用HTTP API. 在Spring Cloud中,使用Feign非常简单——创建一个 ...
- OpenCL Workshop 1 —— 数字音频滤波
Introduction 这两年深度学习大火,Cuda跟着吃红利,OpenCL发展也很快.虽然OpenCL不是事实上的标准,但是作为开放标准,适应性是很强的,除了显卡之外,CPU/FPGA上都可以执行 ...
- LC 926. Flip String to Monotone Increasing
A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), ...
- 问题解决:fatal error C1083: 无法打开包括文件:No such file or directory
fatal error C1083: 无法打开包括文件:No such file or directory将别的工程直接用VS2010打开出现了该问题,此时必须检查是不是: 1. 如果要引入的这些.h ...
- Spring Security在登录验证中增加额外数据(如验证码)
在使用Spring Security框架过程中,经常会有这样的需求,即在登录验证时,附带增加额外的数据,如验证码.用户类型等.下面将介绍如何实现. 注:我的工程是在Spring Boot框架基础上的, ...
- [Spark News] Spark + GPU are the next generation technology
一.资源:Spark进行机器学习,支持GPU From:https://my.oschina.net/u/2306127/blog/1602291 为了使用Spark进行机器学习,支持GPU是必须的, ...
- 中crontab定时器里的"2>&1"含义解释
*/1 * * * * /usr/local/php/bin/php posts.php >> /data/logs/audit_bbsposts.log 2>&1 & ...