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 ...
随机推荐
- Django QuerySet API---数据库接口
基本的创建与查询 # -*- coding: utf-8 -*- from django.http import HttpResponse from TestModel.models import T ...
- .net core 不同地区时间相互转换
.net core 不同地区时间相互转换 //韩国时间转换成当前时间 //value=需要转换的时间 //Korea Standard Tim 韩国时间 //China Standard Time 中 ...
- NULL-safe equal null 索引 空字符串
小结 1. mysql> INSERT INTO my_table (phone) VALUES (NULL); 有手机号但是不知道 mysql> INSERT INTO my_table ...
- (009)每日SQL学习:Oracle各个键说明(转)
原文地址:http://www.agiledata.org/essays/keys.html 本文概述关系数据库中为表指定主键的策略.主要关注于何时使用自然键或者代理键的问题.有些人会告诉你应该总是使 ...
- 洛谷P2865
感觉此题可作为严格次短路的模板,因此来写一写 Description 给定 \(n\) 个点,\(r\) 条双向道路,求从 \(1\) 号点到 \(n\) 号点的严格次短路 Solution 维护两个 ...
- java 静态资源,非静态资源,父类子类,构造方法之间的初始化循序
java面试经常被问静态资源,非静态资源,父类子类,构造方法之间的执行顺序.下面添加两个类做个测试 class Parent { // 静态变量 public static String p_Stat ...
- 客户端,Scala:Spark查询Phoenix
客户端,Scala:Spark查询Phoenix 1.pom.xml 2.配置文件 2.1config.properties 2.2MyConfig 3.entity实体(与phoenix中的tabl ...
- SpringMVC传递JSON数据
文章目录 一.前后端传递和接收JSON数据 1:是要Ajax默认格式来传递数据(*) 2:使用application/json格式来传递数据 二.spring-web.xml中需要如下配置 一.前后端 ...
- 通过spring statemmachine 自定义构建属于自己的状态机(两种方式)
spring 的stateMachine 相对于当前的版本,还是比较新颖的,但是对于合适的业务场景,使用起来还是十分的方便的.但是对于官网提供的文档,讲解的是十分的精简,要想更深入的了解其内部架构,只 ...
- thymeleaf第二篇:理解原理并为后面springboot进行整合进行铺垫
官方入门之从虚拟商店理解thymeleaf 参考文档: 简单使用Thymeleaf API渲染模板生成静态页面 邮件通知改造之Thymeleaf渲染模板生成静态页面--看懂会帮助理解springboo ...