回答

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(通常)只在一个线程中运行。