reactjs - 并行解决多个获取请求
问题描述
我正在尝试在react-native
. 但我没有得到预期的响应数据。我错误地集成了什么?
async componentDidMount() {
try {
let [res1, res2] = await Promise.all([
fetch(apiUrl1),
fetch(apiUrl2),
]);
console.warn(res1);
console.warn(res2);
}
catch(err) {
console.warn(err);
};
}
这是我得到的奇怪反应。
{"_bodyBlob": {"_data": {"__collector": [Object], "blobId": "4", "offset": 0, "size": 661}}, "_bodyInit": {"_data": {"__collector": [Object], "blobId": "", "offset": 0, "size": 661}}, "headers": {"map": {"cache-control": "no-store, no-cache, must-revalidate", "cf-cache-status": "DYNAMIC", "cf-ray": "5", "content-type": "application/json; charset=utf-8", "date": "Thu, 09 Jan 2020 12:15:40 GMT", "expect-ct": "max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"", "expires": "", "pragma": "no-cache", "server": "cloudflare", "set-cookie": "ci_session=; expires=; Max-Age=7200; path=/; HttpOnly"}}, "ok": true, "status": 200, "statusText": undefined, "type": "default", "url": "apiurl"}
解决方案
如果您的响应以 json 格式出现,请使用以下代码
let [res1, res2] = await Promise.all([
fetch(apiUrl1).then(response => response.json()),
fetch(apiUrl2).then(response => response.json()),
]);
推荐阅读
- android - 领域突然开始在没有问题的代码中生成编译错误_ClassNameRealmProxy
- laravel - 如何根据laravel中的下拉菜单显示数据值?
- python - 包的导入错误,我是否缺少 __init__.py
- c++ - 通过元素指针的无序映射作为键对向量进行排序
- c# - dll 和 pdb 未在 bin/debug 文件夹中创建
- python - 使用 Keras flow_from_dataframe 解决多输出回归问题
- registration - 使用 pip 找不到 Elastix
- python - 使用 numpy 和 matplotlib 从 CSV 文件生成热图 - 如何在轴上显示负数
- javascript - 如何在 React 应用程序(CRA)中从 jQuery 加载 $ 函数?
- node.js - Hapi 抱怨缺少安装的插件依赖项