async.parallel`s function is working after another function has invoked callback function with error, why?
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 |
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 '18 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 '18 at 18:40
Ok, I see. That's true, but thesetTimeoutstill gets called after 5 seconds regardless.
– JohnnyHK
Nov 21 '18 at 18:47
exactly, why?????????????
– Rasul1996
Nov 21 '18 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 '18 at 18:57
add a comment |
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 '18 at 18:22
Rasul1996Rasul1996
166
166
Why do you thinkBBBBBwould not be output?
– JohnnyHK
Nov 21 '18 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 '18 at 18:40
Ok, I see. That's true, but thesetTimeoutstill gets called after 5 seconds regardless.
– JohnnyHK
Nov 21 '18 at 18:47
exactly, why?????????????
– Rasul1996
Nov 21 '18 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 '18 at 18:57
add a comment |
Why do you thinkBBBBBwould not be output?
– JohnnyHK
Nov 21 '18 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 '18 at 18:40
Ok, I see. That's true, but thesetTimeoutstill gets called after 5 seconds regardless.
– JohnnyHK
Nov 21 '18 at 18:47
exactly, why?????????????
– Rasul1996
Nov 21 '18 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 '18 at 18:57
Why do you think
BBBBB would not be output?– JohnnyHK
Nov 21 '18 at 18:35
Why do you think
BBBBB would not be output?– JohnnyHK
Nov 21 '18 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 '18 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 '18 at 18:40
Ok, I see. That's true, but the
setTimeout still gets called after 5 seconds regardless.– JohnnyHK
Nov 21 '18 at 18:47
Ok, I see. That's true, but the
setTimeout still gets called after 5 seconds regardless.– JohnnyHK
Nov 21 '18 at 18:47
exactly, why?????????????
– Rasul1996
Nov 21 '18 at 18:56
exactly, why?????????????
– Rasul1996
Nov 21 '18 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 '18 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 '18 at 18:57
add a comment |
1 Answer
1
active
oldest
votes
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 |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 '18 at 0:36
Jonathan TsaiJonathan 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.
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 '18 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 '18 at 18:40
Ok, I see. That's true, but the
setTimeoutstill gets called after 5 seconds regardless.– JohnnyHK
Nov 21 '18 at 18:47
exactly, why?????????????
– Rasul1996
Nov 21 '18 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 '18 at 18:57