Angular 6 - call multiple APIs synchronously












0















I have a simple function that call an API to get all categories of story. The response simply like this:



{"page":1,"pageSize":1,"totalRecords":39,"nextPageUrl":"http://dkm.services/api/story/categories?SiteId=1c0ad0e3-8c70-4a0c-9dd1-3285d0b243f2&Page=2&PageSize=10","items":[{"id":"96c9219a-b1a1-4dc5-9dbc-021720d71ab0","name":"Đông Phương","url":"http://truyenfull.vn/the-loai/dong-phuong/"}]}


Now I need to follow the nextPageUrl property to invoke the next call until all category are obtained (nextPageUrl = null)
The function will return an array of category, then it will be called in another component in my app.



How can I chain this multiple calls?



Thank you all.










share|improve this question























  • Promises? developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…

    – JuanDM
    Jan 3 at 13:42











  • yes I can use promise only if I know exactly how many calls I need to invoke, but in my case I don't. I only know what is the current api's url and what is the next (returned from the previous response)

    – Nam HP
    Jan 3 at 13:57


















0















I have a simple function that call an API to get all categories of story. The response simply like this:



{"page":1,"pageSize":1,"totalRecords":39,"nextPageUrl":"http://dkm.services/api/story/categories?SiteId=1c0ad0e3-8c70-4a0c-9dd1-3285d0b243f2&Page=2&PageSize=10","items":[{"id":"96c9219a-b1a1-4dc5-9dbc-021720d71ab0","name":"Đông Phương","url":"http://truyenfull.vn/the-loai/dong-phuong/"}]}


Now I need to follow the nextPageUrl property to invoke the next call until all category are obtained (nextPageUrl = null)
The function will return an array of category, then it will be called in another component in my app.



How can I chain this multiple calls?



Thank you all.










share|improve this question























  • Promises? developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…

    – JuanDM
    Jan 3 at 13:42











  • yes I can use promise only if I know exactly how many calls I need to invoke, but in my case I don't. I only know what is the current api's url and what is the next (returned from the previous response)

    – Nam HP
    Jan 3 at 13:57
















0












0








0








I have a simple function that call an API to get all categories of story. The response simply like this:



{"page":1,"pageSize":1,"totalRecords":39,"nextPageUrl":"http://dkm.services/api/story/categories?SiteId=1c0ad0e3-8c70-4a0c-9dd1-3285d0b243f2&Page=2&PageSize=10","items":[{"id":"96c9219a-b1a1-4dc5-9dbc-021720d71ab0","name":"Đông Phương","url":"http://truyenfull.vn/the-loai/dong-phuong/"}]}


Now I need to follow the nextPageUrl property to invoke the next call until all category are obtained (nextPageUrl = null)
The function will return an array of category, then it will be called in another component in my app.



How can I chain this multiple calls?



Thank you all.










share|improve this question














I have a simple function that call an API to get all categories of story. The response simply like this:



{"page":1,"pageSize":1,"totalRecords":39,"nextPageUrl":"http://dkm.services/api/story/categories?SiteId=1c0ad0e3-8c70-4a0c-9dd1-3285d0b243f2&Page=2&PageSize=10","items":[{"id":"96c9219a-b1a1-4dc5-9dbc-021720d71ab0","name":"Đông Phương","url":"http://truyenfull.vn/the-loai/dong-phuong/"}]}


Now I need to follow the nextPageUrl property to invoke the next call until all category are obtained (nextPageUrl = null)
The function will return an array of category, then it will be called in another component in my app.



How can I chain this multiple calls?



Thank you all.







api asynchronous angular6






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 13:38









Nam HPNam HP

165




165













  • Promises? developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…

    – JuanDM
    Jan 3 at 13:42











  • yes I can use promise only if I know exactly how many calls I need to invoke, but in my case I don't. I only know what is the current api's url and what is the next (returned from the previous response)

    – Nam HP
    Jan 3 at 13:57





















  • Promises? developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…

    – JuanDM
    Jan 3 at 13:42











  • yes I can use promise only if I know exactly how many calls I need to invoke, but in my case I don't. I only know what is the current api's url and what is the next (returned from the previous response)

    – Nam HP
    Jan 3 at 13:57



















Promises? developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…

– JuanDM
Jan 3 at 13:42





Promises? developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…

– JuanDM
Jan 3 at 13:42













yes I can use promise only if I know exactly how many calls I need to invoke, but in my case I don't. I only know what is the current api's url and what is the next (returned from the previous response)

– Nam HP
Jan 3 at 13:57







yes I can use promise only if I know exactly how many calls I need to invoke, but in my case I don't. I only know what is the current api's url and what is the next (returned from the previous response)

– Nam HP
Jan 3 at 13:57














1 Answer
1






active

oldest

votes


















0














You need recursive promise, something like this ...



function getCategories(nextPageUrl, categories) {
return getCategoriesService(nextPageUrl)
.then((res) => {
if(res.nextPageUrl) {
categories.push(res.categories)
return getCategories(res.nextPageUrl, categories);
}
});
}





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%2f54023417%2fangular-6-call-multiple-apis-synchronously%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









    0














    You need recursive promise, something like this ...



    function getCategories(nextPageUrl, categories) {
    return getCategoriesService(nextPageUrl)
    .then((res) => {
    if(res.nextPageUrl) {
    categories.push(res.categories)
    return getCategories(res.nextPageUrl, categories);
    }
    });
    }





    share|improve this answer




























      0














      You need recursive promise, something like this ...



      function getCategories(nextPageUrl, categories) {
      return getCategoriesService(nextPageUrl)
      .then((res) => {
      if(res.nextPageUrl) {
      categories.push(res.categories)
      return getCategories(res.nextPageUrl, categories);
      }
      });
      }





      share|improve this answer


























        0












        0








        0







        You need recursive promise, something like this ...



        function getCategories(nextPageUrl, categories) {
        return getCategoriesService(nextPageUrl)
        .then((res) => {
        if(res.nextPageUrl) {
        categories.push(res.categories)
        return getCategories(res.nextPageUrl, categories);
        }
        });
        }





        share|improve this answer













        You need recursive promise, something like this ...



        function getCategories(nextPageUrl, categories) {
        return getCategoriesService(nextPageUrl)
        .then((res) => {
        if(res.nextPageUrl) {
        categories.push(res.categories)
        return getCategories(res.nextPageUrl, categories);
        }
        });
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 15:55









        Žarko SelakŽarko Selak

        648818




        648818
































            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%2f54023417%2fangular-6-call-multiple-apis-synchronously%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

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas

            Can't read property showImagePicker of undefined in react native iOS