how to run sequence functions in javascript [duplicate] - javascript

This question already has an answer here:
How can I write an async method in JavaScript when posting or looping? [closed]
(1 answer)
Closed 9 years ago.
How to run sequence functions in JavaScript:
I've 4 functions like this:
function myfunction()
{
callfunction1();
callfunction2();
}
In callfunction1:
function callfunction1()
{
callfunction3();
}
My problem is in "myfunction" when I call "callfunction1",callfunction1 is run and it call "callfunction3" but when "callfunction3" haven't finish,"callfunction2" is running.Have anyway to wait "callfunction3" finish then "callfunction2" is run ?

JavaScript is single-threaded, callfunction2 will only run after callfunction3 has compeletely finished.
However, if callfunction3 contains some AJAX or other asynchronous operation, then that will always happen after callfunction2 (because of the single-threaded thing, it can't start the asynchronous callback until after the synchronous stuff is done). Anything that relies on the result of an asynchronous function MUST either be in the function itself, or called by said function.

Related

Why is 'await' necessary for javascript? [duplicate]

This question already has answers here:
Understanding the Event Loop
(4 answers)
Closed 1 year ago.
My understanding is that javascript is single-threaded, meaning it will only execute one line at a time.
For the following code, why is 'await' necessary? I think it will still execute fetch before it moves to the next line:
const res = await fetch('http://testurl.com');
Can someone explain the importance of await in single-thread languages?
Thank you
Two types of functions can be 'paused' mid-function,
Async functions
Generator functions
So while you are correct, only 1 line will be executed at a time, await basically tells Javascript:
This function is now paused, and resume this function once this promise resolves.
While the function is paused, other code can run. But the 'only 1 line of code will run'-rule still holds.

Node.js setTimeout in forever loop [duplicate]

This question already has answers here:
Callback of an asynchronous function is never called
(1 answer)
setTimeout not working inside infinite loop
(5 answers)
Closed 5 years ago.
Can't explain nodejs behavior. I have code:
while (true) {
setTimeout(() => console.log(1), 0)
}
And this script just hanging...
How come? I've been thinking setTimeout is non-blocking and asynchronous, and nodejs use timers event loop phase for scheduling setTimeout callbacks... but it seems like event loop blocked...
Your while loop is infinite. It will just keep setting timeouts and never exiting the loop. Since JavaScript is single-threaded, the code for the timeouts won't run until the current code is finished, and since the while loop never finishes, the timeouts don't run.
If you want to spam the console with the number 1 using timeouts, which it looks like you are trying to do, you would have to set the next timeout in the current timeout's callback:
function timeoutFunc() {
console.log(1);
setTimeout(timeoutFunc, 0);
}
timeoutFunc();

"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!

Trying to access a variable inside a jquery function [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
I am trying to access a variables inside a $.post jquery method. The code I have so far is below:
var fromDatabase;
$.post( "../read.php", function( data ) {
fromDatabase = JSON.parse(data);
console.log(fromDatabase); //this works fine
return fromDatabase;
});
console.log(fromDatabase); // but this gives me 0.
I am trying to get the from database variable so i tried to declare it outside the function to no avail.
Thank you.
You can't - you must continue program execution from within the callback, not immediately after the asynchronous $.post call.
You cannot return from an asynchronous function, that's the nature of asynchronicity. Instead, after your value is available (the callback function is called) you must work with the data within the scope of that function.
Perhaps a good starting point would be some Ajax tutorial. If you want more, simply google for JavaScript async.

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