Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is meant by 'pause' when async function await expression 'pauses' execution?

I am very new to the concept of asyncronicity in Javascript and I wanted to make sure I am not misinterpreting what I am reading.

Consider this pseudo code in some Angular app:

async ngOnInit() {

this.responseObject = await this.myService.myGetRequest();

//do some more stuff down here

}

My understanding is that the ngOnInit() will 'pause' or stop execution at await, and NOT execute the code below that line until the promise object(data) is returned? Correct?

like image 991
MadCatm2 Avatar asked Dec 05 '25 00:12

MadCatm2


1 Answers

The async function declaration will return a Promise that is resolved with the returned valued from your function call.

If you add the await expression, it will stop the async execution and wait until your promise is resolve to continue executing the other instructions in your code, somehow making it behave like a 'synchronous' function.

Do some reading over here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

like image 138
bws Avatar answered Dec 07 '25 12:12

bws



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!