Javascript 等待所有响应,然后调用函数
我有多个URL来调用数据,我想在所有URL都响应并收到数据后调用函数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)}) } }
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);
您的第一组承诺是否得到了您希望加载图像的响应,或者是否执行了异步操作?如果没有,则在解析第一组后,您不需要任何更多的承诺。您的第一组承诺是否得到您希望加载图像的响应,或者执行异步操作?如果没有,在第一组问题解决后,你就不需要任何承诺了。太好了!非常感谢!完美的非常感谢!