The sad thing is you can't. Array.prototype.forEach
is a higher level function, it just calls the given callback, but will not and cannot take generators. I mean you can give a generator as generators are just normal functions, but they will not run, and you can't yield values.
The second thing is that you would just yield timeoutId-s, and I am pretty sure you wanted to wait for 1500 ms.
So you will have to get rid of forEach and use for..of instead, and will have to write a delay function, using async/await it would look like:
function delay(time, value) {
return new Promise(resolve => { setTimeout(() => { resolve(value); }, time); });
}
async function main() {
for (var item of ['1', '2', '3']) {
await delay(1000);
console.log(item);
}
}
main().then(null, e => console.error(e));
You can transpile it with babel.
If you wish to use regular node callbacks, that is a bit harder and not so nice, but definitely possible. If you are allowed to choose, I suggest you to use the async/await way.