T
T
tuxx2017-07-03 14:13:10
JavaScript
tuxx, 2017-07-03 14:13:10

What's wrong with the async\await code?

There is a code:

let Load = (item, config) =>
{
  return new Promise((resolve, reject) =>
  {
    let link = "URL";
    
    Request.get(link, {timeout: 5 * second}, (err, response, body) =>
    {
      if(err)
      {
        reject(err);
      }
      else
      {
        resolve(body);
      }
    });
  });
};

let AwaitLoad = async (item, config) =>
{
  let result = await LoadSteamPrice(item, config);
  console.log(result);

  return result;
};

The call to AwaitLoad goes to forEach:
Info.forEach(item =>
{
  let result = AwaitLoad(item, config);
  console.log(result);
});

// далее должен быть еще код по обработке Info и всякого другого в синхронном порядке

That is, I need to consistently contact certain addresses and get information from there. And add some of the received information to the elements from Info. console.log in forEach returns Promise { <pending> } , but at the same time, after processing console.log in AwaitLoad, the desired execution result is changed.
I can't load them all in parallel via Promise.all, because already 3-4 parallel requests return the response 429 Too many requests, and with a serial poll I can send at least a hundred requests.
After this forEach I need to process Info further in synchronous order.
Node.js v8.1.2

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nicholas, 2017-07-03
@tuxx

The loop also needs await. And in the function itself, you can remove it if it's justreturn foo(a,b)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question