1、使用nodejs,外層有var enter = Promise.all([Promise.all([innerPromise...])...]).then(console.log("problem happend"))嵌套來(lái)解析二維數(shù)據(jù),內(nèi)層的數(shù)組的innerPromise實(shí)際上也是一個(gè)startPromise.then(return secendPromise)結(jié)構(gòu)。結(jié)果secendPromise在外層Promise.all Fulfilled后才執(zhí)行. 即problem happend 會(huì)在secendPromise還未執(zhí)行時(shí)就已經(jīng)輸出了。難道Promise.all不是promise數(shù)組都Fulfilled了之后才會(huì)被Fulfilled?2、具體的示例如下。輸入數(shù)據(jù)型如[[cat,[nameId1,nameId2,nameId3...]]...],cat對(duì)應(yīng)多個(gè)nameId。這種二維數(shù)據(jù)使用了兩個(gè)Promise.all嵌套解析。執(zhí)行結(jié)果是數(shù)據(jù)庫(kù)操作的第一步insertCategoryAttrName完成后client就關(guān)閉了,后續(xù)的查詢和插入操作無(wú)法完成。3、我這邊懷疑和Promise.all的機(jī)制有關(guān)系,于是把dbInsert的二維數(shù)組展開(kāi)成一維數(shù)組,使用一層Promise.all發(fā)現(xiàn)client可以在合適的時(shí)機(jī)關(guān)閉4 、因此基本可以判斷是Promise.all nest導(dǎo)致對(duì)insertCategoryAttrName后面then執(zhí)行狀態(tài)的感知。具體的原因仍然想不明白,求高手指點(diǎn)。5、本地用setTimeout模擬場(chǎng)景做了下測(cè)試..好吧,代碼書(shū)寫問(wèn)題..insertChain方法內(nèi)層的Promise.all根本就沒(méi)返回..不過(guò)借這個(gè)機(jī)會(huì)看了Promise的源碼也算是收獲
promise.all 嵌套無(wú)法保證內(nèi)層的Promise執(zhí)行順序
慕容3067478
2018-08-02 11:10:52