7
我期望下面的代碼在控制檯上打印一個數字,然後等待一秒鐘,然後再打印另一個數字。相反,它會立即打印所有10個號碼,然後等待十秒鐘。創建承諾鏈的正確方式是什麼?在for循環中創建承諾鏈
function getProm(v) {
return new Promise(resolve => {
console.log(v);
resolve();
})
}
function Wait() {
return new Promise(r => setTimeout(r, 1000))
}
function createChain() {
let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
let chain = Promise.resolve();
for (let i of a) {
chain.then(()=>getProm(i))
.then(Wait)
}
return chain;
}
createChain();