🔧 Write promises chaining using async/await.
Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to
async
and await
are JavaScript keywords that enable us to work with promises in a more simple way.
async
If you define a function that returns a promise, and this promise will always only resolve, in this case, instead of constructing a promise, you can use async
keyword before the function and directly return the resolved value.
Let's clarify by an example 👇
You can replace the second function with the first one. Using async
keyword before the function will make this function return an implicit promise behind the scenes. The value you actually return from this function will act as if it is a result passed through hidden resolve
function.
And in turns, we can use chaining on the returned promise.
What about await
keyword ?
await
await
keyword is used instead of then
function, which means we can let go of writing chains.
Be careful about 🤓
await
keyword must be used before promises, you can't await for anything expect promise.await
keyword must be used inside a function withasync
keyword.
Summary:
-
async
replaces constructing promises. -
await
replacesthen
. -
async
can be used alone, butawait
must be used insideasync
function.
Error handling of promises is a great topic. It will be clarified in another article, just wait for it 🔥.
...
🔧 Write promises chaining using async/await.
📈 77.1 Punkte
🔧 Programmierung
🔧 Using Promises and Async/Await in JavaScript
📈 49.25 Punkte
🔧 Programmierung
🔧 Asynchronous JavaScript: Promises Async Await!
📈 44.62 Punkte
🔧 Programmierung
🔧 Callbacks, Promises, and Async-Await
📈 44.62 Punkte
🔧 Programmierung