用await进行阻塞:
function getList(i){
return new Promise((resolve,reject)=>{
setTimeout(()=>{
resolve(i)
},1000)
})
}
async function breakAwait(){
for(let i = 0;i<3;i++){
let data = await getList(i);
console.log('data是',data);
if(data===1){
break;
}
}
}
breakAwait();
打印如下:
data是 0
data是 1