Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.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 等待所有响应,然后调用函数_Javascript_Api_Promise_Fetch - Fatal编程技术网

Javascript 等待所有响应,然后调用函数

Javascript 等待所有响应,然后调用函数,javascript,api,promise,fetch,Javascript,Api,Promise,Fetch,我有多个URL来调用数据,我想在所有URL都响应并收到数据后调用函数 var promises = urls.map(url => fetch(url)); Promise.all(promises).then(response => { for (var i = 0; i < response.length; i++) { response[i].json().then(data => { dataReceived.push(data.rows)}) } }

我有多个URL来调用数据,我想在所有URL都响应并收到数据后调用函数

var promises = urls.map(url => fetch(url));

Promise.all(promises).then(response => {
 for (var i = 0; i < response.length; i++) {
  response[i].json().then(data => { dataReceived.push(data.rows)})
 }
 }).then(dataReceived=> {

  doThisFucntion(withAllTheData);

 });
var promises=url.map(url=>fetch(url));
承诺。所有(承诺)。然后(响应=>{
对于(变量i=0;i{dataReceived.push(data.rows)})
}
})。然后(dataReceived=>{
完成此功能(包括所有数据);
});
我想我只需要再加一个承诺。好吧,但我不知道该怎么做


谢谢

可能是这样的:

var promises = urls.map(url => fetch(url));

Promise.all(promises)
  .then(response => Promise.all(response.map(resp=>resp.json())))
  .then(data=>data.map(element=>element.rows))
  .then(dataReceived=> {    
    doThisFucntion(withAllTheData);
  });

可能是这样的:

var promises = urls.map(url => fetch(url));

Promise.all(promises)
  .then(response => Promise.all(response.map(resp=>resp.json())))
  .then(data=>data.map(element=>element.rows))
  .then(dataReceived=> {    
    doThisFucntion(withAllTheData);
  });

尝试使用与URL相同的技巧。只需接受每个响应,将其映射到您想要的内容,并将所有这些都放在一个
承诺中。所有

var promises = urls.map(url => fetch(url));

Promise.all(promises).then(response => {
    return Promise.all(response.map(resp => resp.json().then(data => data.rows)));
}).then(dataReceived => {
    // dataReceived is an array where each entry is one of the 'data.rows' from before.
    doThisFucntion(dataReceived);
});

尝试使用与URL相同的技巧。只需接受每个响应,将其映射到您想要的内容,并将所有这些都放在一个
承诺中。所有

var promises = urls.map(url => fetch(url));

Promise.all(promises).then(response => {
    return Promise.all(response.map(resp => resp.json().then(data => data.rows)));
}).then(dataReceived => {
    // dataReceived is an array where each entry is one of the 'data.rows' from before.
    doThisFucntion(dataReceived);
});

你真的不需要另一个
承诺。全部
。只需将主体解析和属性提取承诺链接放在您已有的
映射
回调中:

var promises = urls.map(url =>
  fetch(url).then(response => response.json()).then(data => data.rows)
);

Promise.all(promises).then(doThisFunction);

你真的不需要另一个
承诺。全部
。只需将主体解析和属性提取承诺链接放在您已有的
映射
回调中:

var promises = urls.map(url =>
  fetch(url).then(response => response.json()).then(data => data.rows)
);

Promise.all(promises).then(doThisFunction);

您的第一组承诺是否得到了您希望加载图像的响应,或者是否执行了异步操作?如果没有,则在解析第一组后,您不需要任何更多的承诺。您的第一组承诺是否得到您希望加载图像的响应,或者执行异步操作?如果没有,在第一组问题解决后,你就不需要任何承诺了。太好了!非常感谢!完美的非常感谢!