function main(){
console.log('A');
var p = new Promise(function(res, rej) {
runWhileLoopForNSeconds(20);
console.log('While loop is done');
res(1);
});
console.log('C');
}
main();
function runWhileLoopForNSeconds(sec){
let start = Date.now(), now = start;
while (now - start < (sec*1000)) {
now = Date.now();
}
}
Promise阻止整个功能,直到完成为止。它不是异步运行代码