Home > Software design >  is posible sleep in Azure Function Nodejs?
is posible sleep in Azure Function Nodejs?

Time:11-26

I have calculated Request Units (RU) needed for 50 update operations per seconds on: https://cosmos.azure.com/capacitycalculator/

but those 50 update operations need 1 second to complete

Thus,

I need to sleep 1 second while inserting into a mongodb database with bulk operation (group of update operations)

is this posible in azure functions with nodejs?

I have tried this code

sleep(milliseconds) {
    return new Promise(resolve => setTimeout(resolve, milliseconds))
}

but doesnt work.

any similar situation?

CodePudding user response:

Usually we use delay async function for waiting a block to rerun after 1000 milliseconds.

Below is one of the sample we make us of it on our regular basis to add delay to our logic. As you've not provided your entire code, make sure you are following the below structure.

function RunAfterOneSec() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('resolved');
    }, 1000);
  });
}

async function asyncCall() {
  console.log('calling');
  const result = await RunAfterOneSec();
  console.log(result);
  // expected output: "resolved"
}

asyncCall();

Or you can use with help of node sleep package and we can use it as below:

var sleep = require('sleep'); 
sleep.sleep(n)
  • sleep.sleep(n): sleep for n seconds
  • sleep.msleep(n): sleep for n miliseconds
  • sleep.usleep(n): sleep for n microseconds (1 second is 1000000 microseconds)
  • Related