const delayFunction = async () => {
const startTime = new Date().getTime();
const duration = 10000;
console.log('START')
while (new Date().getTime() - startTime < duration) {
// this loop runs for 10 seconds
}
console.log('STOP');
};
const starterFunction = async () => {
console.log('1');
delayFunction();
console.log('2');
return 'DONE'
};
console.log(starterFunction())
所以现在我想要如下输出:
1
2
完成
开始
停止
如果有人知道如何通过异步编程实现这一点,请帮助我。
我尝试创建一个新的承诺并尝试执行其中的代码,但即使这样似乎也不起作用。
const delayFunction = async () => {
return new Promise((resolve,reject) => {
const startTime = new Date().getTime();
const duration = 10000;
console.log('START')
while (new Date().getTime() - startTime < duration) {
// this loop runs for 10 seconds
}
resolve(console.log('STOP'));
});
};
const duration = 10000;
console.log('START');
return new Promise((resolve) => {
setTimeout(() => {
console.log('STOP');
resolve();
}, duration);
});
};
const starterFunction = async () => {
console.log('1');
console.log('2');
await delayFunction();
return 'DONE';
};
(async () => {
await starterFunction();
})();