Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Promise.allsolited访问fetch/http调用的响应值?_Javascript_Promise_Fetch - Fatal编程技术网

Javascript 如何使用Promise.allsolited访问fetch/http调用的响应值?

Javascript 如何使用Promise.allsolited访问fetch/http调用的响应值?,javascript,promise,fetch,Javascript,Promise,Fetch,我正在尝试使用Promise.AllSetted调用3个URL,并在完成所有操作后处理结果。但是,我似乎无法访问响应的值,它们是JSON。这是我的密码: let urlList = ['http...', 'http...', 'http...']; let fetches = urlList.map(url => fetch(url)); Promise.allSettled(fetches) .then((results) => { // (*) re

我正在尝试使用Promise.AllSetted调用3个URL,并在完成所有操作后处理结果。但是,我似乎无法访问响应的值,它们是JSON。这是我的密码:

let urlList = ['http...', 'http...', 'http...'];

let fetches = urlList.map(url => fetch(url));
Promise.allSettled(fetches)
    .then((results) => { // (*)

        results.forEach((result, num) => {

            if (result.status == "fulfilled") {
                //result.value is a response object and I cannot seem to access the response.body or the actual text/json value of the response
                // in chrome developer tools, I can see the results I want, just can't seem to read them here
                console.log(result.value);
            }
            else if (result.status == "rejected") {
                console.log(`Failed Calling: ${urls[num]}\n\treason: ${result.reason}`);
            }

        });

        //call other method with combined results...
    })
    .catch((err) => {
        alert(err);
    });
console.log输出:

我错过了什么?提前感谢您的帮助

更改此选项:

let fetches = urlList.map(url => fetch(url));
为此:

let fetches = urlList.map(url => fetch(url).then(res => res.json()));
这样,您的
Promise.allsolited()
将为您提供一个承诺数组,这些承诺将解析为最终值,而不仅仅是响应头对象,因此您的
result.value
属性将是每个承诺的最终值