Skip to content

Instantly share code, notes, and snippets.

@mynameiskreang
Created November 12, 2019 03:36
Show Gist options
  • Save mynameiskreang/ae74b25d449a2d7b7a33d2d08f4b6063 to your computer and use it in GitHub Desktop.
Save mynameiskreang/ae74b25d449a2d7b7a33d2d08f4b6063 to your computer and use it in GitHub Desktop.
loop async/await
async function f() {
let sumPass = 0;
for (let i = 0; i < 100; i++) {
const isPass = await f1(i);
if (isPass) {
sumPass++;
// await makeAsyncRequest(i);
const ps = new Promise((resolve, reject) => {
setTimeout(() => {resolve(i);}, 100);
});
await ps.then((psOut) => {
console.log(psOut);
});
}
}
console.log('sumPass', sumPass);
}
function makeAsyncRequest(i) {
return new Promise((resolve, reject) => {
console.log(i);
setTimeout(resolve, 100);
});
}
async function f1(i) {
return i % 10 === 0;
}
f();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment