ASYNC AWAIT
The
async
keyword is used to write functions that handle asynchronous actions. We wrap our asynchronous logic inside a function prepended with the async
keyword- An
async
function will return in one of three ways: - If there’s nothing returned from the function, it will return a promise with a resolved value of
undefined
. - If there’s a non-promise value returned from the function, it will return a promise resolved to that value.
- If a promise is returned from the function, it will simply return that promise
await
is an operator: it returns the resolved value of a promise.
await
halts, or pauses, the execution of our async
function until a given promise is resolved.
When used properly in
yesAwait()
, the variable value
was assigned the resolved value of the myPromise()
promise, whereas in noAwait()
, value
was assigned the promise object itself.
NOTES: To execute a file, we can type
node someFileName.js
in the terminal and press enter.
With
async...await
, we use try...catch
statements for error handling. - Another way to take advantage of concurrency when we have multiple promises which can be executed simultaneously is to
await
aPromise.all()
.
Review:
What I learned today?
I learned to use the keywords to make the functions work and run the way I want it to be. However, I think I should still learned more about this if I have time, as I am not 100% mastered in this.
What I'll be doing tomorrow?
No comments:
Post a Comment