async.parallel`s function is working after another function has invoked callback function with error, why?












1















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?










share|improve this question























  • 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











  • 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











  • 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
















1















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?










share|improve this question























  • 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











  • 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











  • 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














1












1








1








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 18:22









Rasul1996Rasul1996

166




166













  • 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











  • 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











  • 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











  • 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











  • 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 users 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 users email and username are not taken by someone else in async.parallel but doesnt work

– Rasul1996
Nov 21 '18 at 18:57












1 Answer
1






active

oldest

votes


















2














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:




  1. Both setTimeout() are set.

  2. 2000 milliseconds later, console.log("AAA") and cb(a, null) are called.


  3. cb(a, null) has an error. So the main callback is called, and async.parallel() ends.

  4. But the story does not end here. The second setTimeout() is already set. Calling the main callback will not clear the timeout.


  5. console.log("BBBBB") and cb(b, null) are called. This is why you see the output BBBBB.

  6. Because the main callback is already called, calling cb(b, null) will not do anything.






share|improve this answer
























    Your Answer






    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    2














    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:




    1. Both setTimeout() are set.

    2. 2000 milliseconds later, console.log("AAA") and cb(a, null) are called.


    3. cb(a, null) has an error. So the main callback is called, and async.parallel() ends.

    4. But the story does not end here. The second setTimeout() is already set. Calling the main callback will not clear the timeout.


    5. console.log("BBBBB") and cb(b, null) are called. This is why you see the output BBBBB.

    6. Because the main callback is already called, calling cb(b, null) will not do anything.






    share|improve this answer




























      2














      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:




      1. Both setTimeout() are set.

      2. 2000 milliseconds later, console.log("AAA") and cb(a, null) are called.


      3. cb(a, null) has an error. So the main callback is called, and async.parallel() ends.

      4. But the story does not end here. The second setTimeout() is already set. Calling the main callback will not clear the timeout.


      5. console.log("BBBBB") and cb(b, null) are called. This is why you see the output BBBBB.

      6. Because the main callback is already called, calling cb(b, null) will not do anything.






      share|improve this answer


























        2












        2








        2







        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:




        1. Both setTimeout() are set.

        2. 2000 milliseconds later, console.log("AAA") and cb(a, null) are called.


        3. cb(a, null) has an error. So the main callback is called, and async.parallel() ends.

        4. But the story does not end here. The second setTimeout() is already set. Calling the main callback will not clear the timeout.


        5. console.log("BBBBB") and cb(b, null) are called. This is why you see the output BBBBB.

        6. Because the main callback is already called, calling cb(b, null) will not do anything.






        share|improve this answer













        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:




        1. Both setTimeout() are set.

        2. 2000 milliseconds later, console.log("AAA") and cb(a, null) are called.


        3. cb(a, null) has an error. So the main callback is called, and async.parallel() ends.

        4. But the story does not end here. The second setTimeout() is already set. Calling the main callback will not clear the timeout.


        5. console.log("BBBBB") and cb(b, null) are called. This is why you see the output BBBBB.

        6. Because the main callback is already called, calling cb(b, null) will not do anything.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 0:36









        Jonathan TsaiJonathan Tsai

        1464




        1464
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            How to pass form data using jquery Ajax to insert data in database?

            National Museum of Racing and Hall of Fame

            Guess what letter conforming each word