Promise nested then execute order All In One
Promise nested then execute order All In One
Promise nested then

nested Promise
- not return new Promise
遇到 Promise 立即执行,Promise 后面紧跟着的第一个 then 也马上执行;
后面的 then 按照 Promise 创建的先后顺序,依次执行;
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-10-01
* @modified
*
* @description
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
* @best_solutions
*
*/
const log = console.log;
const promise = new Promise((resolve, reject) =>{
log(1);
resolve(1);
});
// then((json) => log(3, json), (err) => log(err));
promise
.then((v) => {
// not new Promise ️
// return new Promise((ok, err) => {
new Promise((ok, err) => {
log(2);
log(`v =`, v);
// resolve callback, ok
ok(``);
// reject callback, err
err(``)
}).then((v) => {
log(3);
log(`v =`, v, `\n`);
return v;
}).then((v) => {
log(4);
log(`v =`, v, `\n`);
return v;
});
// 遇到 Promise 立即执行,Promise 后面紧跟着的第一个 then 也马上执行,后面的 then 按照 Promise 创建的先后顺序,依次执行 then!
return v;
})
.then((v) => {
log(5);
log(`v =`, v);
return v;
})
.then((v) => {
log(6);
log(`v =`, v);
return v;
});
/*
1
2
v = 1
3
v =
5
v = 1
4
v =
6
v = 1
*/
- return new Promise
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-10-01
* @modified
*
* @description
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
* @best_solutions
*
*/
const log = console.log;
const promise = new Promise((resolve, reject) =>{
log(1);
resolve(1);
});
// then((json) => log(3, json), (err) => log(err));
promise
.then((v) => {
// return new Promise
return new Promise((ok, err) => {
log(2);
log(`v =`, v);
// resolve callback, ok
ok(``);
// reject callback, err
err(``)
}).then((v) => {
log(3);
log(`v =`, v);
return v;
}).then((v) => {
log(4);
log(`v =`, v);
return v;
});
// return v;
})
.then((v) => {
log(5);
log(`v =`, v);
return v;
})
.then((v) => {
log(6);
log(`v =`, v);
return v;
});
/*
1
2
v = 1
3
v =
4
v =
5
v =
6
v =
*/
refs
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
Promise nested then execute order All In One的更多相关文章
- Filter execute order in asp.net web api
https://stackoverflow.com/questions/21628467/order-of-execution-with-multiple-filters-in-web-api Som ...
- 执行 innerHTML 里的 <script>
原文:执行 innerHTML 里的 <script> 背景 有时候我们会有把一整段 HTML 动态塞进页面的需求,例如渲染了一个模板,从服务器端获取了一段广告代码等.一般情况下我们使用 ...
- (二)Netty源码学习笔记之服务端启动
尊重原创,转载注明出处,原文地址:http://www.cnblogs.com/cishengchongyan/p/6129971.html 本文将不会对netty中每个点分类讲解,而是一个服务端启 ...
- netty4.0.x源码分析—bootstrap
Bootstrap的意思就是引导,辅助的意思,在编写服务端或客户端程序时,我们都需要先new一个bootstrap,然后基于这个bootstrap调用函数,添加eventloop和handler,可见 ...
- a* products
Experience of black-box testing on set-top-boxes/IP-connected devices, games consoles and tablets ht ...
- [Javascript] Promise-based functions should not throw exceptions
Source You can also start a chain of then() method calls via Promise.resolve() and execute the synch ...
- Jasper_crosstab_columngroup header position config - (headerPosition="Stretch")
Position of Totals RowThe totalPosition attribute controls the appearance of the row that displays t ...
- Java learning notes (1):Basic Knowlege points
Basic Knowlege points: 1: it's necessary that there is only one public class in per .java file 2: .j ...
- [netty源码分析]3 eventLoop 实现类SingleThreadEventLoop职责与实现
eventLoop是基于事件系统机制,主要技术由线程池同队列组成,是由生产/消费者模型设计,那么先搞清楚谁是生产者,消费者内容 SingleThreadEventLoop 实现 public abst ...
随机推荐
- Cannot assign requested address问题总结
Cannot assign requested address问题总结 - 简书 https://www.jianshu.com/p/51a953b789a4 python3 server.pyE07 ...
- 两个报文是如何进行 TCP 分组传输
16 | 如何理解TCP的"流"? https://time.geekbang.org/column/article/132443 TCP 是一种流式协议在前面的章节中,我们讲的都 ...
- py, pyc, pyw, pyo, pyd Compiled Python File (.pyc) 和Java或.NET相比,Python的Virtual Machine距离真实机器的距离更远
https://my.oschina.net/renwofei423/blog/17404 1. PyCodeObject与Pyc文件 通常认为,Python是一种解释性的语言,但是这种说法 ...
- 用好Java中的枚举真的没有那么简单
1.概览 在本文中,我们将看到什么是 Java 枚举,它们解决了哪些问题以及如何在实践中使用 Java 枚举实现一些设计模式. enum关键字在 java5 中引入,表示一种特殊类型的类,其总是继承j ...
- springBoot controller入参LocalDateTime
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss") @DateTimeForma ...
- 在Centos7上安装Python+Selenium+Chrome+Chromedriver
1.下载Chrome 上一篇文章已经演示过了Python+Selenium+Firefox+Geckodriver安装步骤并通过自动化脚本打开百度 因此当前只需要安装Chrome和Chromedriv ...
- QT之——QTableWidget拖拽单元格并替换内容(进阶)
所需待重写函数: [virtual] bool QObject::eventFilter(QObject *watched, QEvent *event); /* * Filters events i ...
- Language Guide (proto3) | proto3 语言指南(三)默认值
默认值 解析消息时,如果编码的消息不包含特定的单数元素,则解析对象中的相应字段将设置为该字段的默认值.这些默认值是特定于类型的: string:默认值为空字符串 bytes:默认值为空字节 boole ...
- 11月份 chrome 标签整理
Spring MVC框架相关 Java Web开发 和 linux下开发 汇总 项目源码 优秀的音视频开源框架 常用软件的下载 学习资源或网站 最后分享一些以前收藏的优秀博客 这两天经过3次面试,很幸 ...
- linux下安装 zookeeper-3.4.9并搭建集群环境
本文主要记录作者在实践过程中实现在centos7环境下安装zookeeper并搭建集群的详细步骤,关于zookeeper本文将不做详细介绍,安装步骤详情如下: 前提准备:3台linux服务器(因为zo ...