基本承诺问题:
console.log('Promise START');
function makeFullJSON(time) {
return new Promise((resolve, reject) => {
setTimeout(resolve, time, [time]);
})
}
var p1 = makeFullJSON(1000);
var p2 = makeFullJSON(500);
var p3 = makeFullJSON(750);
p1.then(array => {
console.log('Promise 1 complete', array);
});
p2.then(array => {
console.log('Promise 2 complete', array);
});
p3.then(array => {
console.log('Promise 3 complete', array);
});
Promise.all([p1, p2, p3]).then(arrayOfAllResolvedValues => {
console.log('Array of resolved values:', arrayOfAllResolvedValues);
});
console.log('Promise END');
代码输出为:
Promise START
Promise END
Promise 2 complete [ 500 ]
Promise 3 complete [ 750 ]
Promise 1 complete [ 1000 ]
Array of resolved values: [ [ 1000 ], [ 500 ], [ 750 ] ]
如何重写代码,输出为:
Promise START
Promise 2 complete [ 500 ]
Promise 3 complete [ 750 ]
Promise 1 complete [ 1000 ]
Array of resolved values: [ [ 1000 ], [ 500 ], [ 750 ] ]
Promise END
答案 0 :(得分:5)
完成后您想要发生的任何事情都会转移到您传递给的箭头功能。
console.log('Promise START');
function makeFullJSON(time) {
return new Promise((resolve, reject) => {
setTimeout(resolve, time, [time]);
})}
var p1 = makeFullJSON(1000);
var p2 = makeFullJSON(500);
var p3 = makeFullJSON(750);
p1.then(array => {
console.log('Promise 1 complete', array);});
p2.then(array => {
console.log('Promise 2 complete', array););
p3.then(array => {
console.log('Promise 3 complete', array);});
Promise.all([p1, p2, p3]).then(arrayOfAllResolvedValues => {
console.log('Array of resolved values:', arrayOfAllResolvedValues);
console.log('Promise END');
});
为了放弃立即执行程序,并开始编写仅在所有3个promises解决之后才会发生的代码,因为它听起来你想要发生,那么我建议在代码的正下方创建一个新函数,以包含代码您希望在解决后发生这种情况,并将该函数传递给:Promise.all([p1, p2, p3]).then(newFunctionName)
。您可能更容易以这种方式对其进行可视化,至少在您习惯于思考它是如何正常工作之前。
答案 1 :(得分:2)
首先修复语法错误。然后将console.log移动到整个过程结束的位置:
console.log('Promise START');
function makeFullJSON(time) {
return new Promise((resolve, reject) => {
setTimeout(resolve, time, [time]);
})}
var p1 = makeFullJSON(1000);
var p2 = makeFullJSON(500);
var p3 = makeFullJSON(750);
p1.then(array => {
console.log('Promise 1 complete', array);});
p2.then(array => {
console.log('Promise 2 complete', array);}); // fixed syntax error here
p3.then(array => {
console.log('Promise 3 complete', array);});
Promise.all([p1, p2, p3]).then(arrayOfAllResolvedValues => {
console.log('Array of resolved values:', arrayOfAllResolvedValues);
console.log('Promise END');
});
答案 2 :(得分:1)
如果放置console.log('Promise END')
的明显答案没有浮动你的船,为什么不呢?
//didnt change anything at all above here
Promise.all([p1, p2, p3]).then(arrayOfAllResolvedValues => {
console.log('Array of resolved values:', arrayOfAllResolvedValues);
}).then(() => {
console.log('Promise END');
});
如果要在完成所有承诺后对操作进行排序,则必须按照打印数组值时的then()
顺序进行排序
答案 3 :(得分:0)
使用async
/ await
:
(async () => {
console.log('Promise START');
function makeFullJSON(time) {
return new Promise((resolve, reject) => {
setTimeout(resolve, time, [time]);
})}
var p1 = makeFullJSON(1000);
var p2 = makeFullJSON(500);
var p3 = makeFullJSON(750);
p1.then(array => {
console.log('Promise 1 complete', array);});
p2.then(array => {
console.log('Promise 2 complete', array);});
p3.then(array => {
console.log('Promise 3 complete', array);});
console.log('Array of resolved values:', await Promise.all([p1, p2, p3]));
console.log('Promise END');
})();