程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了多个顺序fetch()承诺大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决多个顺序fetch()承诺?

开发过程中遇到多个顺序fetch()承诺的问题如何解决?下面主要结合日常开发的经验,给出你关于多个顺序fetch()承诺的解决方法建议,希望对你解决多个顺序fetch()承诺有所启发或帮助;

您可以使用递归

function fetchNextJson(Json_url) {
    return fetch(Json_url, {
            method: 'get'
        })
        .then(function(response) {
            return response.Json();
        })
        .then(function(Json) {
            results.push(Json);
            return Json.Pagination.NextPage.href 
                   ? fetchNextJson(Json.Pagination.NextPage.href)
                   : results
        })
        .catch(function(err) {
            console.log('error: ' + error);
        });
}


var next_Json_url = 'http://localhost:3000/one';
var results = [];

fetchNextJson(Json_url).then(function(res) {
  console.log(res)
})

解决方法

我必须做一个序列的fetch()承诺:我一次只有1个网址,这意味着只有1个fetch()诺言。每次我收到一个json时,其中一个都包含另一个json的网址,因此我必须做出另一个fetch()承诺。

我可以处理多个诺言,但是在这种情况下,我做不到Promise.all(),因为我没有所有的URL,只有一个。

这个例子不起作用,全部冻结。

function fetchNextJson(json_url) 
{
    return fetch(json_url,{
            method: 'get'
        })
        .then(function(response) {
            return response.json();
        })
        .then(function(json) {
            console.log(json);
            return json;
        })
        .catch(function(err) {
            console.log('error: ' + error);
        });
}


function getItems(next_json_url) 
{
    if (!(next_json_url)) return;

    get_items = fetchNextJson(next_json_url);

    interval = $q.when(get_items).then(function(response) {
        console.log(response);
        next_json_url = response.Pagination.NextPage.Href;
    });

    getItems(next_json_url);
}


var next_json_url = 'http://localhost:3000/one';

getItems(next_json_url);

大佬总结

以上是大佬教程为你收集整理的多个顺序fetch()承诺全部内容,希望文章能够帮你解决多个顺序fetch()承诺所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: