Angular 6 - 同步调用多个API

问题描述 投票:0回答:1

我有一个简单的函数调用API来获取所有类别的故事。响应就像这样:

{"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/"}]}

现在我需要按照nextPageUrl属性来调用下一个调用,直到获得所有类别(nextPageUrl = null)该函数将返回一个类别数组,然后它将在我的应用程序中的另一个组件中调用。

如何链接这多个电话?

谢谢你们。

api asynchronous angular6
1个回答
0
投票

你需要递归的承诺,像这样......

function getCategories(nextPageUrl, categories) {
  return getCategoriesService(nextPageUrl)
    .then((res) => {
      if(res.nextPageUrl) {
        categories.push(res.categories)
        return getCategories(res.nextPageUrl, categories);
      } 
    });
}
© www.soinside.com 2019 - 2024. All rights reserved.