Skip to content

Instantly share code, notes, and snippets.

@saeidabdi
Last active January 4, 2023 07:31
Show Gist options
  • Save saeidabdi/24bec268c1ca6832036d4284f58362a4 to your computer and use it in GitHub Desktop.
Save saeidabdi/24bec268c1ca6832036d4284f58362a4 to your computer and use it in GitHub Desktop.
test run nodejs parallel
/**
* 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