Skip to content

Instantly share code, notes, and snippets.

@ColinFendrick
Last active February 13, 2020 20:58
Show Gist options
  • Save ColinFendrick/2a28b95edb800cd81e9f32480d436068 to your computer and use it in GitHub Desktop.
Save ColinFendrick/2a28b95edb800cd81e9f32480d436068 to your computer and use it in GitHub Desktop.
(async () => {
const waitFor = (ms) => new Promise(r => setTimeout(r, ms));
const array = [1, 2, 3];
const obj = {a: 1, b: 2, c: 3};
for (let index = 0; index < array.length; index++) { // passes
await waitFor(50);
console.log('for-let:', array[index]);
}
console.log('for-let done ---PASSES---');
array.forEach(async n => { // fails
await waitFor(50);
console.log('forEach', n);
});
console.log('forEach done ---FAILS---');
for (const n of array) { // passes
await waitFor(50);
console.log('for of', n)
}
console.log('for of done ---PASSES---');
await Promise.all(array.map(n => { // passes
waitFor(50);
console.log('Promise.all', n);
}));
console.log('Promise.all done ---PASSES---')
for (const prop in obj) { // passes
await waitFor(50);
console.log('for in', obj[prop]);
}
console.log('for in done ---PASSES---');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment