I just have implemented AJAX calls using fetch in react-native. Implementing queuing of these AJAX calls hasn't been very well implemented. Can any one help me?
答案 0 :(得分:0)
如果您想要发出并行请求,可以使用fetch
向您返回承诺的事实,然后您可以使用Promise.all
等待所有承诺的完成。
例如:
var urls = ['http://url1.net', 'http://url2.net'];
var requests = [];
urls.forEach((url)=>{
request = fetch(url); // You can also pass options or any other parameters
requests.push(request);
});
// Then, wait for all Promises to finish. They will run in parallel
Promise.all(requests).then((results) => {
// Results will hold an array with the results of each promise.
}).catch((err)=>{
// Promise.all implements a fail-fast mechanism. If a request fails, the catch method will be called immediately
});
我注意到你添加了多线程'标签。重要的是要注意这个代码不会为你做任何线程,因为JS(通常)只在一个线程中运行。