这是一个定时器函数
const timer = (time) => {
return new Promise((resolve, reject) => {
console.log(`${time} timer start`);
setTimeout(() => {
console.log(`${time} timer end`);
resolve();
}, time);
});
};
Run Code Online (Sandbox Code Playgroud)
我将用“for wait of”和“for of”来调用这个计时器函数。
首先,“等待”
async function runForAwaitOf() {
const times = [300, 100, 700, 500];
for await (let time of times) {
await timer(time);
}
console.log('All the timers end');
}
runForAwaitOf()
Run Code Online (Sandbox Code Playgroud)
二、“为之”
async function runForOf() {
const times = [300, 100, 700, 500];
for (let time of times) {
await timer(time);
} …Run Code Online (Sandbox Code Playgroud) javascript ×1