Why do functions have to be async in order to use await within them?
up vote
-1
down vote
favorite
If I want to await a function, why does that have to be done in an async function only?
If I have this:
Function myFunc()
return await myOtherFunc();
I get an error saying: “await expression is only allowed within an asynchronous function.”
I can understand if myOtherFunc() had to be asynchronous (it doesn't make sense to await an asynchronous function), but why does it care if the calling function is asynchronous or not. You can have a fork in a process within a synchronous function due to a call to an asynchronous function, right? So then why can’t you await that asynchronous function within the synchronous function?
NOTE: my question is NOT a duplicate of Javascript Await/Async Feature - What if you do not have the await word in the function?.
^ that question is asking what happens if await is not used in an async function. I'm asking why the function has to be async in order to use await. My response to DougBug below explains why they are different.
asynchronous async-await
add a comment |
up vote
-1
down vote
favorite
If I want to await a function, why does that have to be done in an async function only?
If I have this:
Function myFunc()
return await myOtherFunc();
I get an error saying: “await expression is only allowed within an asynchronous function.”
I can understand if myOtherFunc() had to be asynchronous (it doesn't make sense to await an asynchronous function), but why does it care if the calling function is asynchronous or not. You can have a fork in a process within a synchronous function due to a call to an asynchronous function, right? So then why can’t you await that asynchronous function within the synchronous function?
NOTE: my question is NOT a duplicate of Javascript Await/Async Feature - What if you do not have the await word in the function?.
^ that question is asking what happens if await is not used in an async function. I'm asking why the function has to be async in order to use await. My response to DougBug below explains why they are different.
asynchronous async-await
Possible duplicate of Javascript Await/Async Feature - What if you do not have the await word in the function?
– emix
Nov 9 at 20:03
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
If I want to await a function, why does that have to be done in an async function only?
If I have this:
Function myFunc()
return await myOtherFunc();
I get an error saying: “await expression is only allowed within an asynchronous function.”
I can understand if myOtherFunc() had to be asynchronous (it doesn't make sense to await an asynchronous function), but why does it care if the calling function is asynchronous or not. You can have a fork in a process within a synchronous function due to a call to an asynchronous function, right? So then why can’t you await that asynchronous function within the synchronous function?
NOTE: my question is NOT a duplicate of Javascript Await/Async Feature - What if you do not have the await word in the function?.
^ that question is asking what happens if await is not used in an async function. I'm asking why the function has to be async in order to use await. My response to DougBug below explains why they are different.
asynchronous async-await
If I want to await a function, why does that have to be done in an async function only?
If I have this:
Function myFunc()
return await myOtherFunc();
I get an error saying: “await expression is only allowed within an asynchronous function.”
I can understand if myOtherFunc() had to be asynchronous (it doesn't make sense to await an asynchronous function), but why does it care if the calling function is asynchronous or not. You can have a fork in a process within a synchronous function due to a call to an asynchronous function, right? So then why can’t you await that asynchronous function within the synchronous function?
NOTE: my question is NOT a duplicate of Javascript Await/Async Feature - What if you do not have the await word in the function?.
^ that question is asking what happens if await is not used in an async function. I'm asking why the function has to be async in order to use await. My response to DougBug below explains why they are different.
asynchronous async-await
asynchronous async-await
edited Nov 10 at 23:27
asked Nov 9 at 19:53
Gibran Shah
174
174
Possible duplicate of Javascript Await/Async Feature - What if you do not have the await word in the function?
– emix
Nov 9 at 20:03
add a comment |
Possible duplicate of Javascript Await/Async Feature - What if you do not have the await word in the function?
– emix
Nov 9 at 20:03
Possible duplicate of Javascript Await/Async Feature - What if you do not have the await word in the function?
– emix
Nov 9 at 20:03
Possible duplicate of Javascript Await/Async Feature - What if you do not have the await word in the function?
– emix
Nov 9 at 20:03
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
The picture in the following article might help explain it to you a bit.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/index
The basic idea though is that the async
keyword allows the function to yield its execution up to a calling function.
As such any function that implements await must be marked async so that it can traverse back up the calling path until the first caller is only waiting on it to complete what it needs to do.
Thanks DougBug. That article answered my question precisely. I didn't realize that the await operator returned control to the calling function. That would be impossible in a synchronous function, right?
– Gibran Shah
Nov 10 at 23:21
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
The picture in the following article might help explain it to you a bit.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/index
The basic idea though is that the async
keyword allows the function to yield its execution up to a calling function.
As such any function that implements await must be marked async so that it can traverse back up the calling path until the first caller is only waiting on it to complete what it needs to do.
Thanks DougBug. That article answered my question precisely. I didn't realize that the await operator returned control to the calling function. That would be impossible in a synchronous function, right?
– Gibran Shah
Nov 10 at 23:21
add a comment |
up vote
0
down vote
accepted
The picture in the following article might help explain it to you a bit.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/index
The basic idea though is that the async
keyword allows the function to yield its execution up to a calling function.
As such any function that implements await must be marked async so that it can traverse back up the calling path until the first caller is only waiting on it to complete what it needs to do.
Thanks DougBug. That article answered my question precisely. I didn't realize that the await operator returned control to the calling function. That would be impossible in a synchronous function, right?
– Gibran Shah
Nov 10 at 23:21
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
The picture in the following article might help explain it to you a bit.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/index
The basic idea though is that the async
keyword allows the function to yield its execution up to a calling function.
As such any function that implements await must be marked async so that it can traverse back up the calling path until the first caller is only waiting on it to complete what it needs to do.
The picture in the following article might help explain it to you a bit.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/index
The basic idea though is that the async
keyword allows the function to yield its execution up to a calling function.
As such any function that implements await must be marked async so that it can traverse back up the calling path until the first caller is only waiting on it to complete what it needs to do.
answered Nov 9 at 20:02
DougBug
362
362
Thanks DougBug. That article answered my question precisely. I didn't realize that the await operator returned control to the calling function. That would be impossible in a synchronous function, right?
– Gibran Shah
Nov 10 at 23:21
add a comment |
Thanks DougBug. That article answered my question precisely. I didn't realize that the await operator returned control to the calling function. That would be impossible in a synchronous function, right?
– Gibran Shah
Nov 10 at 23:21
Thanks DougBug. That article answered my question precisely. I didn't realize that the await operator returned control to the calling function. That would be impossible in a synchronous function, right?
– Gibran Shah
Nov 10 at 23:21
Thanks DougBug. That article answered my question precisely. I didn't realize that the await operator returned control to the calling function. That would be impossible in a synchronous function, right?
– Gibran Shah
Nov 10 at 23:21
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232457%2fwhy-do-functions-have-to-be-async-in-order-to-use-await-within-them%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possible duplicate of Javascript Await/Async Feature - What if you do not have the await word in the function?
– emix
Nov 9 at 20:03