Here is a timer function
const timer = (time) => {
return new Promise((resolve, reject) => {
console.log(`${time} timer start`);
setTimeout(() => {
console.log(`${time} timer end`);
resolve();
}, time);
});
};
I'm going to call this timer function with 'for await of` and 'for of'.
First, 'for await 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()
Second, 'for of'
async function runForOf() {
const times = [300, 100, 700, 500];
for (let time of times) {
await timer(time);
}
console.log('All the timers end');
}
runForOf()
When I ran above codes, I don't see any differences between them.
If they results the sames, why does ECMA make for await of
?