Multiple requests with axios without waiting for all of them to finish in an array list?









up vote
0
down vote

favorite
1












taskList.push(
const data =
url: 'http://$requestUrl?$argsString',
headers:
'Content-Type': 'application/octet-stream',
Authorization: signature //,
//'Content-Length': buffer.length
,
method: 'POST',
data: buffer

return axios(data)
)

try
const data = await Promise.all(taskList)
const res = data.map(d => d.data)
console.log(res)
//ctx.state.data = res
catch (e)
console.log(e)
throw e



How to do not wait all requests finish, because all finish needs too long time. If any request finish, I print it out, it will be very fast for users.










share|improve this question

















  • 2




    No need to use Promise.all() , just add a then() to each request
    – charlietfl
    Nov 11 at 1:18






  • 1




    I am sorry that I didn't get your question. The above comment by @charlietfl is a solution too. What you are doing is making all the asynchronous requests at a time. Putting them with their own then() will make them independent.
    – Akhil Gautam
    Nov 11 at 1:23














up vote
0
down vote

favorite
1












taskList.push(
const data =
url: 'http://$requestUrl?$argsString',
headers:
'Content-Type': 'application/octet-stream',
Authorization: signature //,
//'Content-Length': buffer.length
,
method: 'POST',
data: buffer

return axios(data)
)

try
const data = await Promise.all(taskList)
const res = data.map(d => d.data)
console.log(res)
//ctx.state.data = res
catch (e)
console.log(e)
throw e



How to do not wait all requests finish, because all finish needs too long time. If any request finish, I print it out, it will be very fast for users.










share|improve this question

















  • 2




    No need to use Promise.all() , just add a then() to each request
    – charlietfl
    Nov 11 at 1:18






  • 1




    I am sorry that I didn't get your question. The above comment by @charlietfl is a solution too. What you are doing is making all the asynchronous requests at a time. Putting them with their own then() will make them independent.
    – Akhil Gautam
    Nov 11 at 1:23












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





taskList.push(
const data =
url: 'http://$requestUrl?$argsString',
headers:
'Content-Type': 'application/octet-stream',
Authorization: signature //,
//'Content-Length': buffer.length
,
method: 'POST',
data: buffer

return axios(data)
)

try
const data = await Promise.all(taskList)
const res = data.map(d => d.data)
console.log(res)
//ctx.state.data = res
catch (e)
console.log(e)
throw e



How to do not wait all requests finish, because all finish needs too long time. If any request finish, I print it out, it will be very fast for users.










share|improve this question













taskList.push(
const data =
url: 'http://$requestUrl?$argsString',
headers:
'Content-Type': 'application/octet-stream',
Authorization: signature //,
//'Content-Length': buffer.length
,
method: 'POST',
data: buffer

return axios(data)
)

try
const data = await Promise.all(taskList)
const res = data.map(d => d.data)
console.log(res)
//ctx.state.data = res
catch (e)
console.log(e)
throw e



How to do not wait all requests finish, because all finish needs too long time. If any request finish, I print it out, it will be very fast for users.







javascript node.js axios






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 1:14









Gank

3,58433739




3,58433739







  • 2




    No need to use Promise.all() , just add a then() to each request
    – charlietfl
    Nov 11 at 1:18






  • 1




    I am sorry that I didn't get your question. The above comment by @charlietfl is a solution too. What you are doing is making all the asynchronous requests at a time. Putting them with their own then() will make them independent.
    – Akhil Gautam
    Nov 11 at 1:23












  • 2




    No need to use Promise.all() , just add a then() to each request
    – charlietfl
    Nov 11 at 1:18






  • 1




    I am sorry that I didn't get your question. The above comment by @charlietfl is a solution too. What you are doing is making all the asynchronous requests at a time. Putting them with their own then() will make them independent.
    – Akhil Gautam
    Nov 11 at 1:23







2




2




No need to use Promise.all() , just add a then() to each request
– charlietfl
Nov 11 at 1:18




No need to use Promise.all() , just add a then() to each request
– charlietfl
Nov 11 at 1:18




1




1




I am sorry that I didn't get your question. The above comment by @charlietfl is a solution too. What you are doing is making all the asynchronous requests at a time. Putting them with their own then() will make them independent.
– Akhil Gautam
Nov 11 at 1:23




I am sorry that I didn't get your question. The above comment by @charlietfl is a solution too. What you are doing is making all the asynchronous requests at a time. Putting them with their own then() will make them independent.
– Akhil Gautam
Nov 11 at 1:23












2 Answers
2






active

oldest

votes

















up vote
1
down vote













To run each Promise just add a .then() to each of one them or to the Promise.all, however you will not be able to .map the data after:



try {
const data = Promise.all(taskList).then(() => console.log('finished'));
// this will not wait to run, so data will be probably undefined
const res = data.map(d => d.data)
...


So I'd suggest following the pattern and waiting for them to finish.



If it's taking too long maybe you should try improve the performance of the response in the back-end if that's possible.






share|improve this answer




















  • TypeError: data.map is not a function Because, data has no value yet
    – Gank
    Nov 11 at 7:20

















up vote
1
down vote













In case each response should be logged as soon as it's completed, it should be:



const responses = await Promise.all(taskList.map(async task => 
const data = await task;
console.log(data);
return data;
));





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',
    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%2f53244989%2fmultiple-requests-with-axios-without-waiting-for-all-of-them-to-finish-in-an-arr%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    To run each Promise just add a .then() to each of one them or to the Promise.all, however you will not be able to .map the data after:



    try {
    const data = Promise.all(taskList).then(() => console.log('finished'));
    // this will not wait to run, so data will be probably undefined
    const res = data.map(d => d.data)
    ...


    So I'd suggest following the pattern and waiting for them to finish.



    If it's taking too long maybe you should try improve the performance of the response in the back-end if that's possible.






    share|improve this answer




















    • TypeError: data.map is not a function Because, data has no value yet
      – Gank
      Nov 11 at 7:20














    up vote
    1
    down vote













    To run each Promise just add a .then() to each of one them or to the Promise.all, however you will not be able to .map the data after:



    try {
    const data = Promise.all(taskList).then(() => console.log('finished'));
    // this will not wait to run, so data will be probably undefined
    const res = data.map(d => d.data)
    ...


    So I'd suggest following the pattern and waiting for them to finish.



    If it's taking too long maybe you should try improve the performance of the response in the back-end if that's possible.






    share|improve this answer




















    • TypeError: data.map is not a function Because, data has no value yet
      – Gank
      Nov 11 at 7:20












    up vote
    1
    down vote










    up vote
    1
    down vote









    To run each Promise just add a .then() to each of one them or to the Promise.all, however you will not be able to .map the data after:



    try {
    const data = Promise.all(taskList).then(() => console.log('finished'));
    // this will not wait to run, so data will be probably undefined
    const res = data.map(d => d.data)
    ...


    So I'd suggest following the pattern and waiting for them to finish.



    If it's taking too long maybe you should try improve the performance of the response in the back-end if that's possible.






    share|improve this answer












    To run each Promise just add a .then() to each of one them or to the Promise.all, however you will not be able to .map the data after:



    try {
    const data = Promise.all(taskList).then(() => console.log('finished'));
    // this will not wait to run, so data will be probably undefined
    const res = data.map(d => d.data)
    ...


    So I'd suggest following the pattern and waiting for them to finish.



    If it's taking too long maybe you should try improve the performance of the response in the back-end if that's possible.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 11 at 5:54









    sudodev

    11919




    11919











    • TypeError: data.map is not a function Because, data has no value yet
      – Gank
      Nov 11 at 7:20
















    • TypeError: data.map is not a function Because, data has no value yet
      – Gank
      Nov 11 at 7:20















    TypeError: data.map is not a function Because, data has no value yet
    – Gank
    Nov 11 at 7:20




    TypeError: data.map is not a function Because, data has no value yet
    – Gank
    Nov 11 at 7:20












    up vote
    1
    down vote













    In case each response should be logged as soon as it's completed, it should be:



    const responses = await Promise.all(taskList.map(async task => 
    const data = await task;
    console.log(data);
    return data;
    ));





    share|improve this answer
























      up vote
      1
      down vote













      In case each response should be logged as soon as it's completed, it should be:



      const responses = await Promise.all(taskList.map(async task => 
      const data = await task;
      console.log(data);
      return data;
      ));





      share|improve this answer






















        up vote
        1
        down vote










        up vote
        1
        down vote









        In case each response should be logged as soon as it's completed, it should be:



        const responses = await Promise.all(taskList.map(async task => 
        const data = await task;
        console.log(data);
        return data;
        ));





        share|improve this answer












        In case each response should be logged as soon as it's completed, it should be:



        const responses = await Promise.all(taskList.map(async task => 
        const data = await task;
        console.log(data);
        return data;
        ));






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 7:53









        estus

        63.7k2193201




        63.7k2193201



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244989%2fmultiple-requests-with-axios-without-waiting-for-all-of-them-to-finish-in-an-arr%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

            27

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            Category:Rhetoric