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

This question already has answers here:
What is the JavaScript version of sleep()?
(91 answers)
Javascript sleep/delay/wait function
(6 answers)
Closed 11 months ago.
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?

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)

Related

Pause function until promise is resolved [duplicate]

This question already has answers here:
How to wrap async function calls into a sync function in Node.js or Javascript?
(9 answers)
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
What is the JavaScript version of sleep()?
(91 answers)
Closed 1 year ago.
My function keeps executing indefinitely, although I would have expected it to resume after 10ms. Is there any way I can flush timeouts while I am waiting for the result? Or at least tell JS to execute / resolve other promises before I return my result?
The simple matter is, I NEED the response before I can continue execution. And I can not put my code in an async block whatsoever. I also can not use 'then' since I need to return the raw result. I thought this hack would work, but nope.
As far as my understanding of JS goes, this simply is not possible, but I would love to know.
let test = function () {
let wait = true;
setTimeout(() => {
wait = false
}, 10);
while (wait);
return wait;
}
you can use async/await , to block the flow of the function
example
const mine = async() => {
console.log("start")
await anotherAsyncFunction()
console.log("done")
}

"Real" Sleep() Function without callback [duplicate]

This question already has answers here:
What is the JavaScript version of sleep()?
(91 answers)
Closed 6 years ago.
I know the time.sleep functiom from python. I just want to waot 3 seconds without making a callback. If i had to implemebt a callback i also had to hanfle global vars and more callbacks. I would like to have a function:
function getdata() {
console.log("hello");
Sleep(3000);
console.log("stackoverflow!");
return "my data";}
Please don't give me a solution for my specific example... i would also need sleep in while functions and other things. I searched a lot but i only found (in exanple setTimeout) functions with callback.
It doesn't exist. Javascript isn't designed this way. You need to use a callback and setTimeout. Sorry!

Can i call in javascript a javascript function that runs in background or asynchronously making browser not freeze? [duplicate]

This question already has answers here:
How to run javascript function in "background" / without freezing UI
(7 answers)
Closed 7 years ago.
function test() {
some_code;
}
function do_many_test() {
var i;
for(i=0;i<100;i++){
test();
}
some_code;
some_code;
some_code;
}
The function do_many_test() makes browser stuck for about ten seconds: is there a way to call it making it running in background while the execution flow continues to next code?
If you are using modern browser. you can use
webworker
https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers

How perform an operation after that are passed a specified number of seconds in JavaScript? [duplicate]

This question already has answers here:
How to delay execution in between the following in my javascript
(4 answers)
Difference between setTimeout with and without quotes and parentheses
(6 answers)
Closed 8 years ago.
I am absolutly new in JavaScript and I have some doubt about how to do the following thing:
Into a JavaScript function I have to perform an operation after that are passed 30 seconds. How can I do it?
setTimeout(function()
{
// your code
},30000)
You have to use window.setTimeout

Is there a Sleep/Pause/Wait function in JavaScript? [duplicate]

This question already has answers here:
What is the JavaScript version of sleep()?
(91 answers)
Closed 3 years ago.
Is there a JavaScript function that simulates the operation of the sleep function in PHP — a function that pauses code execution for x milliseconds, and then resumes where it left off?
I found some things here on Stack Overflow, but nothing useful.
You need to re-factor the code into pieces. This doesn't stop execution, it just puts a delay in between the parts.
function partA() {
...
window.setTimeout(partB,1000);
}
function partB() {
...
}
You can't (and shouldn't) block processing with a sleep function. However, you can use setTimeout to kick off a function after a delay:
setTimeout(function(){alert("hi")}, 1000);
Depending on your needs, setInterval might be useful, too.
setTimeout() function it's use to delay a process in JavaScript.
w3schools has an easy tutorial about this function.

Categories

Resources