Promise队列分析

new Promise((resolve,reject)=>{
    console.log("promise1",1) 
    resolve()
}).then(()=>{
    console.log("then11",2)
    new Promise((resolve,reject)=>{
        console.log("promise2",3)
        resolve();
    }).then(()=>{
        console.log("then21",4)
        new Promise((resolve,reject)=>{
            console.log("promise3",5)
            resolve();
        }).then(()=>{
            console.log("then31",7)
        }).then(()=>{
                console.log("then32",9)
        })
    }).then(()=>{
        console.log("then22",8)
    })
}).then(()=>{
    console.log("then12",6)
})  
复制代码

then12要等待then11出队执行返回Promise后才能入队,同理22要等21,32要等31:
promise1执行=>then11入队=>结束 // 输出:1
=>then11出队=>promise2执行=>then21入队=>返回11promise // 输出:2=>3
=>then12入队=>结束
=>then21出队=>promise3执行=>then31入队=>返回21promise // 输出:4=>5
=>then22入队=>结束
=>then12出队=>then31出队=>返回31promise // 输出:6=>7
=>then32入队=>结束
=>then22出队=>then32出队 // 输出:8=>9
javascript

1 => +11 // 1java

11 => +21 => +12 // 2, 3segmentfault

21 => +31 => +22 // 4, 5promise

12 => 31 => +32 // 6, 7ui

22 => 32 // 8, 9spa

new Promise((resolve,reject)=>{
    console.log("promise1")
    resolve()
}).then(()=>{
    console.log("then11")
    new Promise((resolve,reject)=>{
        console.log("promise2")
        resolve()
    }).then(()=>{
        console.log("then21")
    }).then(()=>{
        console.log("then23")
    })
}).then(()=>{
    console.log("then12")
})
new Promise((resolve,reject)=>{
    console.log("promise3")
    resolve()
}).then(()=>{
    console.log("then31")
})
复制代码

promise1 => promise3 => +then11 => +then31 // promise1 promise3
code

then11 => promise2 => +then21 => +then12 // then11 promise2ip

then31 => then21 => +then23 // then31 then21get

then12 => then23 // then12 then23string

segmentfault.com/a/119000001…

相关文章
相关标签/搜索