friends. Recently, I began to study Noda. I came up with an asynchronous multithreaded parser with a limited number of simultaneous downloads as a learning task. In this regard, the question is: Why is this code executed correctly in the first case (ie, asynchronously, the results are output after all data came from api), and in the second case, with a limited number of downloads, does an array of promises return in response?

Here is a working example:

const axios = require('axios'); //It works async function go2() { try { console.time('1st'); const urls = [ { 'url': 'https://randomuser.me/api', 'method': 'get' }, { 'url': 'https://uinames.com/api', 'method': 'get' }]; var promises = await urls.map(async function (currentValue, index, array) { return axios(currentValue); }); // await all three promises to come back and destructure the result into their own variables var array = await Promise.all(promises).catch(function(err) { console.log(err.message); // some coding error in handling happened });; const results=array.map(function (currentValue, index, array) { return currentValue.status; }); console.log(results); console.timeEnd('1st'); return results; } catch (e) { console.error(e); // } } go2(); 

Here is a non-working example:

 async function go3() { try { console.time('1st'); const MAX_SIMULTANEOUS_DOWNLOADS = 2; const urls = [ { 'url': 'https://stats.nba.com/stats/teamyearbyyearstats?LeagueID=00&SeasonType=Playoffs&TeamID=1610612759&PerMode=PerGame', 'method':'get' }, { 'url': 'https://api.github.com/users/wesbos', 'method':'get' }, { 'url': 'https://randomuser.me/api', 'method':'get' }, { 'url': 'https://uinames.com/api', 'method':'get' } ]; let subarray = []; //массив в который будет выведен результат. // Разбиваем массив на чанки, чтобы сразу все ссылки не грузить за один заход for (let i = 0; i <Math.ceil(urls.length/MAX_SIMULTANEOUS_DOWNLOADS); i++){ subarray[i] = urls.slice((i*MAX_SIMULTANEOUS_DOWNLOADS), (i*MAX_SIMULTANEOUS_DOWNLOADS) + MAX_SIMULTANEOUS_DOWNLOADS); } //Здесь должен по идее сработать await, но нет. var dataFromRequest =await subarray.map(async function (currentValue, index, array) { var promises = await currentValue.map(async function (currentValue1, index1, array) { return axios(currentValue1); }); // await all three promises to come back and destructure the result into their own variables var responses=await Promise.all(promises).catch(function(err) { console.log(err.message); // some coding error in handling happened }); const results=await responses.map(function (currentValue1, index1, array) { console.log(index1, currentValue1.data); // some coding error in handling happened return currentValue1.status; }); return results; }); console.log(dataFromRequest); console.timeEnd('1st'); return dataFromRequest; } catch (e) { console.error(e); // } } go3(); 

The response from the server in the second case:

[Promise {}, Promise {}]

  • something await and where it is necessary and where it is not necessary stumbled. - Grundy

2 answers 2

await should apply to Promise .

map - returns an array , so there is no point in front of the map to put await .

Instead, you need to pass it, for example, to Promise.all and apply await to the result.

    The correct answer was given by Grundy - really turning around in Promise.all helped. Just in case, showing the correct version of the code, all of a sudden someone will come in handy:

     async function go3() { try { console.time('1st'); const MAX_SIMULTANEOUS_DOWNLOADS = 2; const urls = [ { 'url': 'https://stats.nba.com/stats/teamyearbyyearstats?LeagueID=00&SeasonType=Playoffs&TeamID=1610612759&PerMode=PerGame', 'method':'get' }, { 'url': 'https://api.github.com/users/wesbos', 'method':'get' }, { 'url': 'https://randomuser.me/api', 'method':'get' }, { 'url': 'https://uinames.com/api', 'method':'get' } ]; let subarray = []; //массив в который будет выведен результат. // Разбиваем массив на чанки, чтобы сразу все ссылки не грузить за один заход for (let i = 0; i <Math.ceil(urls.length/MAX_SIMULTANEOUS_DOWNLOADS); i++){ subarray[i] = urls.slice((i*MAX_SIMULTANEOUS_DOWNLOADS), (i*MAX_SIMULTANEOUS_DOWNLOADS) + MAX_SIMULTANEOUS_DOWNLOADS); } var dataFromRequest = await Promise.all(subarray.map(async function (currentValue, index, array) { var promises = currentValue.map(async function (currentValue1, index1, array) { return axios(currentValue1); }); // await all three promises to come back and destructure the result into their own variables var responses = await Promise.all(promises).catch(function (err) { console.log(err.message); // some coding error in handling happened }); const results = responses.map(function (currentValue1, index1, array) { console.log(index1, currentValue1.data); // some coding error in handling happened return currentValue1.status; }); return results; }) ); console.log(dataFromRequest); console.timeEnd('1st'); return dataFromRequest; } catch (e) { console.error(e); // } } go3()