1

using the javascript SDK that aws provides.

I have a loop that get executed every 16 seconds, in this loop i get the state of each pipeline I have, which comes out to about 20 pipelines and 20 api calls.

My problem is that when this loop gets executed I sometimes end up with some throttling errors from AWS on some states i try to fetch.

I need to some how add like a 1 or 2 second delay before each call to getPipelineState so im not met with throttling errors

below is the code

this.interval = setInterval(async function () {

                await Promise.all(pipelines.map(async pipeline => {
                    let params = {name: pipeline};
                    let data = await codepipeline.getPipelineState(params).promise();


                     // do stuff with response
                     //some sort of delay before the next pipelines state is fetched

                 }));

            }, 16000);
thicksauce
  • 31
  • 5

2 Answers2

2

You can either call async map function with index and await sleep(index*2000) inside or chain promises through array like this:

this.interval = setInterval(async function () {

    await pipelines.reduce((promise, pipeline) => {
      return promise.then(() => {
        return codepipeline.getPipelineState({
          name: pipeline
        }).promise();
      });
    }, Promise.resolve());

}, 16000);
bigless
  • 2,849
  • 19
  • 31
0

Create a new delay function that returns a promise that is resolved after X milliseconds. Then just await that, here's an example.

this.interval = setInterval(async function() {

  await Promise.all(pipelines.map(async pipeline => {
    let params = {
      name: pipeline
    };
    let data = await codepipeline.getPipelineState(params).promise();


    // do stuff with response
    
    /* my addition */
    await delay(1000);
    
    //some sort of delay before the next pipelines state is fetched
    
  }));

}, 16000);


/* My additional function  */
function delay(milli) {
  return new Promise(function(resolve, reject) {
    setTimeout(resolve, milli);
  });
}
Adam H
  • 1,750
  • 1
  • 9
  • 24