[英]How to fire multiple API calls asynchronously at the same time?
Basically I have a screen that calls multiple APIs at componentDidMount
, and each and every API will fire a network request along with a callback, something as below基本上我有一个在
componentDidMount
调用多个 API 的屏幕,每个 API 都会触发一个网络请求和一个回调,如下所示
componentDidMount() {
new APIManager.fetchBooks(this.handleBooks);
new APIManager.fetchAuthor(this.handleAuthor);
new APIManager.fetchShops(this.handleShops);
}
...
handleBooks = (result) => {
this.setState({ books: result });
}
...
And basically all the fetch
methods works pretty similarly, it accepts a single parameter which is a callback from the component, pseudocode as below基本上所有的
fetch
方法的工作原理都非常相似,它接受一个参数,它是来自组件的回调,伪代码如下
export async function fetchBooks(callback) {
const result = await callNetworkAPI();
callback(result);
}
This was working fine on both Android and iOS at react-native v0.53 but it's not working anymore at v0.59.10 on iOS.这在 React-native v0.53 上在 Android 和 iOS 上都运行良好,但在 iOS 上的 v0.59.10 上不再起作用。
What's happening now is that, fetchBooks
, fetchAuthor
and fetchShops
will all still be triggered but this.handleBooks
, this.handleAuthor
will no longer be triggered.现在发生的是,
fetchBooks
、 fetchAuthor
和fetchShops
仍然会被触发,但this.handleBooks
、 this.handleAuthor
将不再被触发。 It's like they lost their cursor.就像他们失去了光标。
I've made it work such that all API calls has to be using await
, something as below我已经使它工作,以便所有 API 调用都必须使用
await
,如下所示
async componentDidMount() {
const books = await APIManager.fetchBooks();
const authors = await APIManager.fetchAuthor();
const shops = await APIManager.fetchShops();
this.handleBooks(books);
this.handleAuthors(authors);
this.handleShops(shops);
}
And of course updates to my APIManager
as below:当然,我的
APIManager
更新如下:
export async function fetchBooks() {
const result = await callNetworkAPI();
return result;
}
Its working fine, but all 3 API calls cannot be asynchronously anymore because I made it await for the previous API call before proceeding.它工作正常,但所有 3 个 API 调用都不能再异步了,因为我让它在继续之前等待上一个 API 调用。 I'm wondering if there's a better way to handle my problem?
我想知道是否有更好的方法来处理我的问题? Or I'm totally on the right track?
或者我完全在正确的轨道上?
You could use Promise.all
to wait for multiple requests in parallel: 您可以使用
Promise.all
并行等待多个请求:
async componentDidMount() {
const [books, authors, shops] = await Promise.all([
APIManager.fetchBooks(),
APIManager.fetchAuthor(),
APIManager.fetchShops(),
]);
this.handleBooks(books);
this.handleAuthors(authors);
this.handleShops(shops);
}
This however waits for all 3 requests to be completed before calling handleBooks
/ handleAuthors
/ handleShops
. 但是,这会在调用
handleBooks
/ handleAuthors
/ handleShops
之前等待所有3个请求完成。
If you want them to call their handle method directly after they completed, you could use .then()
on the individual fetch calls: 如果希望他们在完成之后立即调用其handle方法,则可以在各个提取调用上使用
.then()
:
async componentDidMount() {
await Promise.all([
APIManager.fetchBooks().then(this.handleBooks),
APIManager.fetchAuthor().then(this.handleAuthors),
APIManager.fetchShops().then(this.handleShops),
]);
// All fetch calls are done now
console.log(this.state);
}
(this works because async
functions always return a promise) (这是有效的,因为
async
函数总是返回promise)
About your problem with the fetch calls, you might be affected by this react-native
bug: 关于您的提取调用问题,您可能会受到以下本
react-native
错误的影响:
Multiple fetch request starts to not resolve or reject promise 多次提取请求开始无法解决或拒绝承诺
According to the github issue page, it should be fixed in version 1.2.1
of react-native
. 根据github问题页面,应该在
react-native
1.2.1
版本中修复它。
Hope so It will help you for multiple API calls simultaneously.希望如此 它可以帮助您同时进行多个 API 调用。
React.useEffect(()=>{
Promise.all([
fetch('https://jsonplaceholder.typicode.com/posts'),
fetch('https://jsonplaceholder.typicode.com/users')
]).then(function (responses) {
// Get a JSON object from each of the responses
return Promise.all(responses.map(function (response) {
return response.json();
}));
}).then(function (data) {
// Log the data to the console
// You would do something with both sets of data here
console.log(data);
}).catch(function (error) {
// if there's an error, log it
console.log(error);
});
},[])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.