async.parallel`s function is working after another function has invoked callback function with error, why?
up vote
1
down vote
favorite
i was learning async library and just tried some codes myself and i am issueing a problem that can`t handle, can you please look at the code down below:)
async.parallel([
function (cb) {
setTimeout(() => {
let a = "asd";
console.log("AAA");
cb(a, null);
}, 2000);
},
function (cb) {
setTimeout( () => {
let b = "dasd";
console.log("BBBBB");
cb(b, null);
}, 5000);
}
], function (error, results) {
console.log("CCC");
console.log("Errors: " + error);
console.log("Results: " + results);
});
I supposed that BBB should NOT output to the screen, but to my surprise it DOES, can you help me understand why?
node.js asynchronous
add a comment |
up vote
1
down vote
favorite
i was learning async library and just tried some codes myself and i am issueing a problem that can`t handle, can you please look at the code down below:)
async.parallel([
function (cb) {
setTimeout(() => {
let a = "asd";
console.log("AAA");
cb(a, null);
}, 2000);
},
function (cb) {
setTimeout( () => {
let b = "dasd";
console.log("BBBBB");
cb(b, null);
}, 5000);
}
], function (error, results) {
console.log("CCC");
console.log("Errors: " + error);
console.log("Results: " + results);
});
I supposed that BBB should NOT output to the screen, but to my surprise it DOES, can you help me understand why?
node.js asynchronous
Why do you thinkBBBBBwould not be output?
– JohnnyHK
Nov 21 at 18:35
because, the doc says that If any of the functions pass an error to its callback, the main callback is immediately called with the value of the error.
– Rasul1996
Nov 21 at 18:40
Ok, I see. That's true, but thesetTimeoutstill gets called after 5 seconds regardless.
– JohnnyHK
Nov 21 at 18:47
exactly, why?????????????
– Rasul1996
Nov 21 at 18:56
just, i wanted to check if the new users email and username are not taken by someone else in async.parallel but doesnt work
– Rasul1996
Nov 21 at 18:57
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
i was learning async library and just tried some codes myself and i am issueing a problem that can`t handle, can you please look at the code down below:)
async.parallel([
function (cb) {
setTimeout(() => {
let a = "asd";
console.log("AAA");
cb(a, null);
}, 2000);
},
function (cb) {
setTimeout( () => {
let b = "dasd";
console.log("BBBBB");
cb(b, null);
}, 5000);
}
], function (error, results) {
console.log("CCC");
console.log("Errors: " + error);
console.log("Results: " + results);
});
I supposed that BBB should NOT output to the screen, but to my surprise it DOES, can you help me understand why?
node.js asynchronous
i was learning async library and just tried some codes myself and i am issueing a problem that can`t handle, can you please look at the code down below:)
async.parallel([
function (cb) {
setTimeout(() => {
let a = "asd";
console.log("AAA");
cb(a, null);
}, 2000);
},
function (cb) {
setTimeout( () => {
let b = "dasd";
console.log("BBBBB");
cb(b, null);
}, 5000);
}
], function (error, results) {
console.log("CCC");
console.log("Errors: " + error);
console.log("Results: " + results);
});
I supposed that BBB should NOT output to the screen, but to my surprise it DOES, can you help me understand why?
node.js asynchronous
node.js asynchronous
asked Nov 21 at 18:22
Rasul1996
166
166
Why do you thinkBBBBBwould not be output?
– JohnnyHK
Nov 21 at 18:35
because, the doc says that If any of the functions pass an error to its callback, the main callback is immediately called with the value of the error.
– Rasul1996
Nov 21 at 18:40
Ok, I see. That's true, but thesetTimeoutstill gets called after 5 seconds regardless.
– JohnnyHK
Nov 21 at 18:47
exactly, why?????????????
– Rasul1996
Nov 21 at 18:56
just, i wanted to check if the new users email and username are not taken by someone else in async.parallel but doesnt work
– Rasul1996
Nov 21 at 18:57
add a comment |
Why do you thinkBBBBBwould not be output?
– JohnnyHK
Nov 21 at 18:35
because, the doc says that If any of the functions pass an error to its callback, the main callback is immediately called with the value of the error.
– Rasul1996
Nov 21 at 18:40
Ok, I see. That's true, but thesetTimeoutstill gets called after 5 seconds regardless.
– JohnnyHK
Nov 21 at 18:47
exactly, why?????????????
– Rasul1996
Nov 21 at 18:56
just, i wanted to check if the new users email and username are not taken by someone else in async.parallel but doesnt work
– Rasul1996
Nov 21 at 18:57
Why do you think
BBBBB would not be output?– JohnnyHK
Nov 21 at 18:35
Why do you think
BBBBB would not be output?– JohnnyHK
Nov 21 at 18:35
because, the doc says that If any of the functions pass an error to its callback, the main callback is immediately called with the value of the error.
– Rasul1996
Nov 21 at 18:40
because, the doc says that If any of the functions pass an error to its callback, the main callback is immediately called with the value of the error.
– Rasul1996
Nov 21 at 18:40
Ok, I see. That's true, but the
setTimeout still gets called after 5 seconds regardless.– JohnnyHK
Nov 21 at 18:47
Ok, I see. That's true, but the
setTimeout still gets called after 5 seconds regardless.– JohnnyHK
Nov 21 at 18:47
exactly, why?????????????
– Rasul1996
Nov 21 at 18:56
exactly, why?????????????
– Rasul1996
Nov 21 at 18:56
just, i wanted to check if the new user
s email and username are not taken by someone else in async.parallel but doesnt work– Rasul1996
Nov 21 at 18:57
just, i wanted to check if the new user
s email and username are not taken by someone else in async.parallel but doesnt work– Rasul1996
Nov 21 at 18:57
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
You are using async.parallel(). All asynchronous tasks will be executed without waiting for each other and the execution order is not guaranteed.
Here's a breakdown on how your script is executed:
- Both
setTimeout()are set. - 2000 milliseconds later,
console.log("AAA")andcb(a, null)are called.
cb(a, null)has an error. So the main callback is called, andasync.parallel()ends.- But the story does not end here. The second
setTimeout()is already set. Calling the main callback will not clear the timeout.
console.log("BBBBB")andcb(b, null)are called. This is why you see the outputBBBBB.- Because the main callback is already called, calling
cb(b, null)will not do anything.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You are using async.parallel(). All asynchronous tasks will be executed without waiting for each other and the execution order is not guaranteed.
Here's a breakdown on how your script is executed:
- Both
setTimeout()are set. - 2000 milliseconds later,
console.log("AAA")andcb(a, null)are called.
cb(a, null)has an error. So the main callback is called, andasync.parallel()ends.- But the story does not end here. The second
setTimeout()is already set. Calling the main callback will not clear the timeout.
console.log("BBBBB")andcb(b, null)are called. This is why you see the outputBBBBB.- Because the main callback is already called, calling
cb(b, null)will not do anything.
add a comment |
up vote
2
down vote
You are using async.parallel(). All asynchronous tasks will be executed without waiting for each other and the execution order is not guaranteed.
Here's a breakdown on how your script is executed:
- Both
setTimeout()are set. - 2000 milliseconds later,
console.log("AAA")andcb(a, null)are called.
cb(a, null)has an error. So the main callback is called, andasync.parallel()ends.- But the story does not end here. The second
setTimeout()is already set. Calling the main callback will not clear the timeout.
console.log("BBBBB")andcb(b, null)are called. This is why you see the outputBBBBB.- Because the main callback is already called, calling
cb(b, null)will not do anything.
add a comment |
up vote
2
down vote
up vote
2
down vote
You are using async.parallel(). All asynchronous tasks will be executed without waiting for each other and the execution order is not guaranteed.
Here's a breakdown on how your script is executed:
- Both
setTimeout()are set. - 2000 milliseconds later,
console.log("AAA")andcb(a, null)are called.
cb(a, null)has an error. So the main callback is called, andasync.parallel()ends.- But the story does not end here. The second
setTimeout()is already set. Calling the main callback will not clear the timeout.
console.log("BBBBB")andcb(b, null)are called. This is why you see the outputBBBBB.- Because the main callback is already called, calling
cb(b, null)will not do anything.
You are using async.parallel(). All asynchronous tasks will be executed without waiting for each other and the execution order is not guaranteed.
Here's a breakdown on how your script is executed:
- Both
setTimeout()are set. - 2000 milliseconds later,
console.log("AAA")andcb(a, null)are called.
cb(a, null)has an error. So the main callback is called, andasync.parallel()ends.- But the story does not end here. The second
setTimeout()is already set. Calling the main callback will not clear the timeout.
console.log("BBBBB")andcb(b, null)are called. This is why you see the outputBBBBB.- Because the main callback is already called, calling
cb(b, null)will not do anything.
answered Nov 22 at 0:36
Jonathan Tsai
1464
1464
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418339%2fasync-parallels-function-is-working-after-another-function-has-invoked-callback%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why do you think
BBBBBwould not be output?– JohnnyHK
Nov 21 at 18:35
because, the doc says that If any of the functions pass an error to its callback, the main callback is immediately called with the value of the error.
– Rasul1996
Nov 21 at 18:40
Ok, I see. That's true, but the
setTimeoutstill gets called after 5 seconds regardless.– JohnnyHK
Nov 21 at 18:47
exactly, why?????????????
– Rasul1996
Nov 21 at 18:56
just, i wanted to check if the new user
s email and username are not taken by someone else in async.parallel but doesnt work– Rasul1996
Nov 21 at 18:57