JavaScript Queuing Promises (ES6)

I'm writing a NodeJS service that requests data from APIs. Under load, I don't want to hammer the API with potentially hundreds of simultaneous requests, so I am trying to queue up the requests so they are executed one-by-one and with a delay between them.

const request = require( 'request' );
class WebService {
  constructor() {
    this.RequestQueue = [];
  }

  _Get( uri, options, reply ) {
    return new Promise( ( resolve, reject ) => {
      request.get( uri, options, ( err, resp, body ) => {
        if ( err )
          reject( err );

        reply( resp );
        resolve( resp );
      } );
    } );
  }

  async onRequest( data, reply ) {
    this.RequestQueue.push( this._Get( data.uri, data.opts, reply ) );
  }

  async execute() {
    while( this.RequestQueue.length > 0 ) {
      var current = this.RequestQueue.shift();
      await current();
      await Utils.Sleep(5000); //promise that resolves after 5 seconds
    }
  }
}

Due to the nature of ES6 promises, they start executing when constructed, so this._Get() inside of the onRequest event returns a promise that is already executing. Is there a clean way to avoid this so that I can properly queue the request for later?

Answer:1

I'm learning electron and I've made an electron app that read and create files. When I start the application with "npm start" or "electron ." it works as intended but when I use the "npm run build" ...

I'm learning electron and I've made an electron app that read and create files. When I start the application with "npm start" or "electron ." it works as intended but when I use the "npm run build" ...

I am new to React. I am trying to display few locations over Google Maps using google-map-react. I am using the below source for it. import React, { Component } from 'react'; import GoogleMapReact ...

I am new to React. I am trying to display few locations over Google Maps using google-map-react. I am using the below source for it. import React, { Component } from 'react'; import GoogleMapReact ...

  1. react array iteration
  2. react array foreach
  3. react array loop
  4. react array foreach render
  5. react array foreach is not a function
  6. react array loop render
  7. react array foreach break
  8. react native array iteration
  9. react js array iteration
  10. react iterate array of objects
  11. react iterate array in render
  12. react iterate array jsx
  13. react iterate array component
  14. react iterate array of strings
  15. react iterate array key
  16. react loop array of objects
  17. react loop array jsx
  18. react loop array map
  19. react loop array of elements

I need a simple way to wait for setTimeout code to finish executing and then run the code that comes after setTimeout. Now the code after loop containing setTimout is executing before loop/setTimout ...

I need a simple way to wait for setTimeout code to finish executing and then run the code that comes after setTimeout. Now the code after loop containing setTimout is executing before loop/setTimout ...

I have a ul with items coming from the loop, and then extra li after that. <ul> <todo-item v-for="(todo,index) in todos" v-bind:todo="todo" :key="index" /> <li :key='"new_item"'>...

I have a ul with items coming from the loop, and then extra li after that. <ul> <todo-item v-for="(todo,index) in todos" v-bind:todo="todo" :key="index" /> <li :key='"new_item"'>...