首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在JSX中渲染2 json

如何在JSX中渲染2 json
EN

Stack Overflow用户
提问于 2018-02-08 15:35:20
回答 1查看 47关注 0票数 0

我有两个json的,我在做两个不同的API调用。

代码语言:javascript
复制
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))

第二名:

代码语言:javascript
复制
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,那么应该如何做呢?任何帮助都会很好。谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-09 05:51:29

Promise.all是你所需要的。基本上,它所做的是将一组承诺合并到一个单独的承诺中,并返回已解析的值--在集合中完成的所有内容。

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48689214

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档