javascript-tutorials/async-await/3_async-await.js

61 lines
2.2 KiB
JavaScript
Raw Normal View History

2024-08-06 12:24:34 +02:00
// https://javascript.info/async-await
// An async function always returns a promise. Other values are wrapped in a resolved promise automatically.
// Async and await are merely syntactic sugar in order to make Promise usage easier
async function f1 () {
2024-08-06 16:02:04 +02:00
return 1
2024-08-06 12:24:34 +02:00
}
2024-08-06 16:02:04 +02:00
f1().then(console.log) // 1
2024-08-06 12:24:34 +02:00
// The keyword await makes JavaScript wait until that promise settles and returns its result.
// It can be used in async functions only.
// Lets emphasize: await literally suspends the function execution until the promise settles,
// and then resumes it with the promise result.
async function f2 () {
const promise = new Promise((resolve, reject) => {
2024-08-06 16:02:04 +02:00
setTimeout(() => resolve('done!'), 1000)
})
const result = await promise // wait until the promise resolves (*)
console.log(result) // "done!"
2024-08-06 12:24:34 +02:00
}
2024-08-06 16:02:04 +02:00
f2()
2024-08-06 12:24:34 +02:00
// The code in the same function after "await"
// is to be intended in the "then()" of the primise. This means
// that after await (but before the completion of the Promise),
// the flow of execution goes out that code block. For example
// consider the following example:
async function exampleAsyncFunction () {
2024-08-06 16:02:04 +02:00
console.log('Before await')
2024-08-06 12:24:34 +02:00
await new Promise(function (resolve, reject) {
2024-08-06 16:02:04 +02:00
setTimeout(() => resolve('done'), 500)
}) // Pauses execution here until the promise resolves.
console.log('After await')
2024-08-06 12:24:34 +02:00
}
2024-08-06 16:02:04 +02:00
console.log('Start')
exampleAsyncFunction()
console.log('End')
2024-08-06 12:24:34 +02:00
// The result will be:
// Start, Before Await, End, After await
// "End" comes before "After Await" because the
// flow of execution goes to the caller
// when await is invoked.
// Questions
//
// Why await only works in async function in javascript?
// https://stackoverflow.com/questions/49640647/why-await-only-works-in-async-function-in-javascript
//
// Why using async-await
// https://stackoverflow.com/questions/42624647/why-use-async-when-i-have-to-use-await
//
// Be mindful: it is not possible to create an async function from scratch (such as
// setInterval o fetch)
// https://stackoverflow.com/questions/61857274/how-to-create-custom-asynchronous-function-in-javascript
//
// Another question that may be interesting
// https://stackoverflow.com/questions/42624647/why-use-async-when-i-have-to-use-await