A small api that delays api requests using async/await with Node.js I spent a good couple hours troubleshooting a bug where the yelp-fusion api said I was sending too many requests per second, so I finally found a way to delay each api request to make it work. Please take a look at the code and give me any tips you have. I'd like to see how this could be done in a better way if possible. 

Here is my server.js where post requests come through:

  //handle post requests for getting yelp data
  app.post('/post', async function(req, res) {
    let yelpInData = []
    let result = null;
    req.body.forEach((item) =&gt; {
      yelpInData.push(item);
    });

    result = await handleYelp(yelpInData);
    //send data back to the front end
    res.send(result);

  });

  //handle yelp api
  async function handleYelp(yelpInData) {
    let yelpData = [];

    for (let i = 0, len = yelpInData.length; i &lt; len; i++) {
      let result = await searchPlace(yelpInData[i]);
      yelpData.push(result);
    }
    //  console.log(yelpData);
    return yelpData;

  }

  function searchPlace(city) {
    //console.log(city)
    return new Promise((resolve) =&gt; {
      setTimeout(() =&gt; {
        let coords = city.coords.lat + "," + city.coords.lng;
        let name = city.name;

        client.search({term: name, location: coords}).then(response =&gt; {

          //name, img url, review count, rating, price, location.display address, is closed,phone
          resolve({
            name: response.jsonBody.businesses[0].name,
            img: response.jsonBody.businesses[0].image_url,
            hours: response.jsonBody.businesses[0].is_closed,
            revcount: response.jsonBody.businesses[0].review_count,
            rating: response.jsonBody.businesses[0].rating,
            price: response.jsonBody.businesses[0].price,
            location: response.jsonBody.businesses[0].location.display_address.toString(),
            phone: response.jsonBody.businesses[0].display_phone,
            url: response.jsonBody.businesses[0].url
          });

        }).catch(e =&gt; {
          console.log(e);
        });
      }, 100);
    })
  }


Is setTimeout a good idea for delaying the api requests to yelp or is there a different way to delay? Here is the git repo if you want to run the web app: https://github.com/capozzic1/knockout_map
setTimeout for a delay is as good as any other approach if you're happy to keep the load on the server. Other option is to separate the processing from the scheduling e.g. offload those particular requests to a scheduling service and then drip feed those back into your app for processing.

From a code point of view, there are some small things you can do to tidy it up a bit.... but the biggest issue I see with your code at the moment is there is absolutely zero error handling. Express does have a default error handler which conveniently catches an errors thrown inside middleware, however, just be wary that you do run the risk of leaking internal server details to the client if you let you just leave your errors to bubble up.

Here's a bit of a tidy up &amp; better error handling

// handle post requests for getting yelp data
app.post('/post', async (req, res, next) =&gt; {
  try {
    // nice use of map makes handleYelp redundant :)
    const searches = req.body.map(x =&gt; searchPlace(x));
    const results = await Promise.all(searches);
    return res.send(results);
  } catch (e) {
    console.error(e); // log internal error
    return next(new Error('Internal Server Error')); // return public error to client
  }
});

function searchPlace(city) {
  return new Promise((resolve, reject) =&gt; {
    // use async / await here too :)
    setTimeout(async () =&gt; {
      try {
        const name = city.name;
        const location = `${city.coords.lat},${city.coords.lng}`;
        const result = await client.search({ name, location });
        const business = result.jsonBody.businesses[0];
        resolve({
          name: business.name,
          img: business.image_url,
          hours: business.is_closed,
          revcount: business.review_count,
          rating: business.rating,
          price: business.price,
          location: business.location.display_address.toString(),
          phone: business.display_phone,
          url: business.url
        });
      } catch (e) {
        return reject(e);
      }
    }, 100);
  })
}