回答

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(通常)只在一個線程中運行。