'How to make event-triggered Cloud Function to "pause" while running? [duplicate]

I have my Nodejs Cloud Function executing some quick tasks upon invocation. I need to delay its termination by 20 seconds. What is the way to do this in the code?



Solution 1:[1]

Assuming you absolutely need this and you will not use this in production, this should be pretty straightforward if this is an asynchronous function.

Just define an async function that will return after an arbitrary amount of time:

async function sleepMS(timeMS) {
    await new Promise(res => setTimeout(res, timeMS))
}

then after all your code runs and before your function returns, call it with the amount of time you wish

await sleepMS(20 * 1000)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abraham Labkovsky