Promise nested then execute order All In One

Promise nested then

nested Promise

  1. 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
*/
  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的更多相关文章

  1. Filter execute order in asp.net web api

    https://stackoverflow.com/questions/21628467/order-of-execution-with-multiple-filters-in-web-api Som ...

  2. 执行 innerHTML 里的 <script>

    原文:执行 innerHTML 里的 <script> 背景 有时候我们会有把一整段 HTML 动态塞进页面的需求,例如渲染了一个模板,从服务器端获取了一段广告代码等.一般情况下我们使用 ...

  3. (二)Netty源码学习笔记之服务端启动

    尊重原创,转载注明出处,原文地址:http://www.cnblogs.com/cishengchongyan/p/6129971.html  本文将不会对netty中每个点分类讲解,而是一个服务端启 ...

  4. netty4.0.x源码分析—bootstrap

    Bootstrap的意思就是引导,辅助的意思,在编写服务端或客户端程序时,我们都需要先new一个bootstrap,然后基于这个bootstrap调用函数,添加eventloop和handler,可见 ...

  5. a* products

    Experience of black-box testing on set-top-boxes/IP-connected devices, games consoles and tablets ht ...

  6. [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 ...

  7. Jasper_crosstab_columngroup header position config - (headerPosition="Stretch")

    Position of Totals RowThe totalPosition attribute controls the appearance of the row that displays t ...

  8. 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 ...

  9. [netty源码分析]3 eventLoop 实现类SingleThreadEventLoop职责与实现

    eventLoop是基于事件系统机制,主要技术由线程池同队列组成,是由生产/消费者模型设计,那么先搞清楚谁是生产者,消费者内容 SingleThreadEventLoop 实现 public abst ...

随机推荐

  1. js12种应该注意的地方

    1. == Javascript有两组相等运算符,一组是==和!=,另一组是===和!==.前者只比较值的相等,后者除了值以外,还比较类型是否相同. 请尽量不要使用前一组,永远只使用===和!==.因 ...

  2. Java面向对象(一)----初次见面

    面向对象 面向过程:根据业务逻辑从上到下写代码 函数式编程:对一些功能的代码封装到函数中,日后无需重复编写,直接调用函数就可以了 面向对象:将所有的功能进行封装,面对的事封装了功能的实体(对象),即面 ...

  3. URI与URL傻傻分不清楚?

    前言 总所周知,缓存是解决Http1.1协议传输性能的问题中最主要的手段. 缓存既可以存在于浏览器上,也可以存在于服务器中. 而影响缓存的Http头部有很多,其中Cache-Control是比较重要的 ...

  4. python3中zip对象的使用

    zip(*iterables) zip可以将多个可迭代对象组合成一个迭代器对象,通过迭代取值,可以得到n个长度为m的元组.其中n为长度最短可迭代对象的元素个数,m为可迭代对象的个数.并且每个元组的第i ...

  5. 深度学习论文翻译解析(十八):MobileNetV2: Inverted Residuals and Linear Bottlenecks

    论文标题:MobileNetV2: Inverted Residuals and Linear Bottlenecks 论文作者:Mark Sandler Andrew Howard Menglong ...

  6. Flutter环境搭建遇坑小结(二)

    在上一节中,已经对Flutter运行中始终卡在Running Gradle task 'assembleDebug'...,做出了解决方案,继续往下运行,但是新的问题又出现了: Failed to i ...

  7. [一天一个进阶系列] - MyBatis基础篇

    前言:一直以来,很多人都是拿来主义,只停留在会使用的阶段,从未去研究挖掘其原理,剖析本质.现在慢慢探讨一下其内幕,抛砖引玉 一.简介 1)常用的持久化框架 Hibernate:是一款Java世界中最著 ...

  8. (十四)整合 ClickHouse数据库,实现数据高性能查询分析

    整合 ClickHouse数据库,实现数据高性能查询分析 1.ClickHouse简介 1.1 数据分析能力 2.SpringBoot整个ClickHouse 2.1 核心依赖 2.2 配属数据源 2 ...

  9. Spark 应用监控告警-Graphite_exporter

    Spark 应用监控告警-Graphite_exporter Spark监控和工具 Web界面 事后查看 REST API 度量 高级工具 一.下载graphite_exporter 1.1 修改gr ...

  10. SparkMLlib—协同过滤推荐算法,电影推荐系统,物品喜好推荐

    SparkMLlib-协同过滤推荐算法,电影推荐系统,物品喜好推荐 一.协同过滤 1.1 显示vs隐式反馈 1.2 实例介绍 1.2.1 数据说明 评分数据说明(ratings.data) 用户信息( ...