Created
November 12, 2019 03:36
-
-
Save mynameiskreang/ae74b25d449a2d7b7a33d2d08f4b6063 to your computer and use it in GitHub Desktop.
loop async/await
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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