我有两个json的,我在做两个不同的API调用。
fetch(`veb/api/roleDetails/${id}`).then(response =>
response.json()
).then(responseJson => {
console.log('responseJson = ' + JSON.stringify(responseJson));
this.setState(() => {
return {
allRoleDetails: responseJson,
}
});
}
).catch(error => console.error('Error:', error))第二名:
fetch(`/api/api/itemTypes`).then(response =>
response.json()
).then(responseJson => {
console.log('responseJson = ' + JSON.stringify(responseJson));
this.setState(() => {
return {
names: responseJson,
}
});
}
).catch(error => console.error('Error:', error))在第一个api调用中,我得到了itemtypeid,我必须从其中进行第二个api调用来获取typeid.Even的名称,如果我得到一个组合的json,那么应该如何做呢?任何帮助都会很好。谢谢
发布于 2018-02-09 05:51:29
Promise.all是你所需要的。基本上,它所做的是将一组承诺合并到一个单独的承诺中,并返回已解析的值--在集合中完成的所有内容。
Promise.all([
fetch(`veb/api/roleDetails/${id}`).then(response => response.json()),
fetch(`/api/api/itemTypes`).then(response => response.json())
]).then(([allRoleDetails, names]) => {
// allRoleDetails is the resolve of your first promise
// names is the resolve of your second promise
}).catch(err => {
// catch error here
});有关Promise.all的更多详细信息可以在这里找到:Objects/Promise/all
https://stackoverflow.com/questions/48689214
复制相似问题