let resolvePromise = new Promise(resolve => {
let resolvedPromise = Promise.resolve()
resolve(resolvedPromise)
})
resolvePromise.then(() => {
console.log('resolvePromise resolved')
})
let resolvePromiseThen = Promise.resolve().then(res => {
console.log('promise1')
})
resolvePromiseThen.then(() => {
console.log('promise2')
}).then(() => {
console.log('promise3')
})
输出是 promise1 promise2 resolvePromise resolved promise3
大佬们求解释
1
he1a2s0 2021-05-31 09:51:50 +08:00
以 https://www.promisejs.org/implementing/ 的实现为例,promise 内部使用 setTimeout(function,0)来调用后续操作,所以基本上可按嵌套关系和顺序来用 setTimeout 替换一下,整段代码使用 setTimeout 来描述应该是:
```typescript setTimeout(()=> { setTimeout(()=>{ console.log('resolvePromise resolved') },0) },0) setTimeout(()=>{ console.log('promise1') },0) setTimeout(()=>{ console.log('promise2') setTimeout(()=>{ console.log('promise3') },0) }) ``` 运行下来会得到同样的输出 |
2
he1a2s0 2021-05-31 09:53:45 +08:00
不知道回复怎么发代码。。格式乱了
|