Last active
January 4, 2023 07:31
-
-
Save saeidabdi/24bec268c1ca6832036d4284f58362a4 to your computer and use it in GitHub Desktop.
test run nodejs parallel
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
/** | |
* test run nodejs parallel | |
*/ | |
function task1() { | |
return new Promise(async (resolve, reject) => { | |
const count = 100; | |
let i = 0; | |
while (i < count) { | |
await sleap(2000) | |
console.log('in tasck 1 ', i); | |
i++; | |
} | |
resolve("task1 is done"); | |
}); | |
} | |
function task2() { | |
return new Promise(async (resolve, reject) => { | |
const count = 100; | |
let i = 0; | |
while (i < count) { | |
await sleap(1000) | |
console.log('in tasck 2 ', i); | |
i++; | |
} | |
resolve("task2 is done"); | |
}); | |
} | |
const sleap = (ms) => new Promise(resolve => setTimeout(resolve, ms)) | |
const tasks = [task1(), task2()]; //add tasks here | |
Promise.all(tasks).then((result) => { | |
console.log(result); | |
}) | |
.catch((error) => { | |
console.error(error); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment