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…

相關文章
相關標籤/搜索