Last active
June 2, 2020 11:04
-
-
Save ramiloif/f2cf9e449eb083d6a3a15f590fd16fca to your computer and use it in GitHub Desktop.
This file contains 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
let number = 0; | |
let temp; | |
const inc = () => new Promise(resolve => { | |
setTimeout(() => { | |
temp = number; | |
number = temp + 1; | |
resolve(); | |
}, 4000) | |
}); | |
const concurrency = 100; | |
const pool = new Array(concurrency); | |
for(let i = 0; i < concurrency; i++) { | |
pool[i] = inc(); | |
} | |
Promise.all(pool).then(() => { | |
console.log(number); | |
}); | |
// prints concurrency value |
This file contains 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
let number = 0; | |
//let temp; | |
const sleep = (seconds) => { | |
return new Promise((resolve) => { | |
setTimeout(() => { | |
resolve(); | |
}, seconds * 1000); | |
}); | |
}; | |
const inc = async () => { | |
let temp = number; | |
await sleep(0); | |
number = temp + 1; | |
} | |
const concurrency = 100; | |
const pool = new Array(concurrency); | |
for(let i = 0; i < concurrency; i++) { | |
pool[i] = inc(); | |
} | |
Promise.all(pool).then(() => { | |
console.log(number); | |
}); | |
// prints 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment