我如何使用 fetch 在 react-native 中实现 AJAX 排队

How can i implement AJAX queuing in react-native using fetch

我刚刚在 react-native 中使用 fetch 实现了 AJAX 调用。 实现这些 AJAX 调用的排队还没有很好地实现。 谁能帮帮我?

如果你想做并行请求,你可以使用fetch returns一个promise给你,然后你可以使用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
});

我注意到您添加了 'multithreading' 标签。重要的是要注意这段代码不会为你做任何线程,因为 JS(通常)只在一个线程中运行。