Comments (1)
找到合理的解释了
new Promise((resolve,reject)=>{
console.log("1")
resolve();
}).then(()=>{
// 外部第1个then
console.log("2")
new Promise((resolve,reject)=>{
console.log("3")
resolve();
}).then(()=>{
// 内部第1个then
console.log("4")
}).then(() => {
// 内部第2个then
console.log("5")
})
}).then((res)=>{
// 外部第2个then
console.log("6")
})
简单来讲就是then
回调的注册需要上一个then
里面的同步代码执行完毕
拿上面的代码来讲,当外部第1个then里的resovle()
执行完毕后,该Promise
的状态已经更改,会将内部第1个then回调添加(注册)到微任务队列中;内部第2个then由于上一个then
回调没有执行完毕,因此不会注册。此时外部第1个then里的同步代码执行完毕,会注册外部第2个then回调
整理一下:then
回调注册的顺序是:外部第1个then --> 内部第1个then --> 外部第2个then --> 内部第2个then
ps: 如果将外部第1个then里的new Promise(xxx)
改为return new Promise(xxx)
的话内部第2个then的注册将早于* 外部第2个then*
from just-now-qa.
Related Issues (18)
- js--将数字转成字符串的问题 HOT 1
- 如何在下班后提升自己 HOT 2
- Vue源码 HOT 4
- nodejs HOT 1
- 如何更简洁明了的回答Vue原理
- Redux connect 包裹 React.memo 和 React.memo 包裹 connect 有什么区别吗?
- 不知道如何设定自己的方向 HOT 1
- vue3.0使用vue-seamless-scroll插件报错问题 HOT 1
- 怎么学习算法?大厂面试都要考算法吗? HOT 1
- 如何提升自己的代码质量 HOT 5
- 求解下方这段 Promise 输出结果 HOT 1
- 如何提升自己?(js) HOT 2
- 简历怎么写项目亮点,项目遇到的困难 HOT 1
- 求left、right的宽度 HOT 2
- 这个为啥会报错啊 HOT 3
- 想开源一套基于vue3的组件库,但是对工具链搭建这块不知道怎么去写去调试,有什么好建议吗 HOT 1
- 上下文、作用域 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from just-now-qa.