2024-08-27 09:23:22 +08:00

35 lines
693 B
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 完成delay函数
// 该函数可以等待一段指定的时间
// 返回Promise
function delay(duration) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, duration);
});
}
// 利用delay函数等待3次每次等待1秒每次等待完成后输出ok
// 等待1秒->ok->等待1秒->ok->等待1秒->ok
(async () => {
for (let i = 0; i < 3; i++) {
await delay(1000);
console.log('ok');
}
})();
// delay(1000)
// .then(() => {
// console.log('ok');
// return delay(1000);
// })
// .then(() => {
// console.log('ok');
// return delay(1000);
// })
// .then(() => {
// console.log('ok');
// });