This commit is contained in:
xfarrow 2024-08-06 12:24:34 +02:00
parent 6f7bf89604
commit f95333868f
3 changed files with 64 additions and 4 deletions

View File

@ -43,7 +43,7 @@ function entryPoint () {
Callbacks are usually meant for asynchronous programming. When
we meet a function that accepts a callback, it is likely an
asynchronous function, such as time, such as
asynchronous function, such as
setTimeout(callback, delay).
Callbacks are not asynchronous by nature, but can be used

View File

@ -0,0 +1,60 @@
// 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 () {
return 1;
}
f1().then(console.log); // 1
// 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) => {
setTimeout(() => resolve('done!'), 1000);
});
const result = await promise; // wait until the promise resolves (*)
console.log(result); // "done!"
}
f2();
// 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 () {
console.log('Before await');
await new Promise(function (resolve, reject) {
setTimeout(() => resolve('done'), 500);
}); // Pauses execution here until the promise resolves.
console.log('After await');
}
console.log('Start');
exampleAsyncFunction();
console.log('End');
// 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

View File

@ -4,9 +4,9 @@ https://stackoverflow.com/questions/7131991/asynchronous-and-synchronous-terms
La parola "sincrono" in contesto informatico vuol dire "sincronizzato", ovvero
il chiamante deve aspettare la risposta del chiamato, mentre
"async" vuol dire "non sincronizzato".
Ciò vuol dire sincronizzato (o NON sincronizzato) con altre porzioni di codice.
La definizione da dizionario invece differisce.
"async" vuol dire "non sincronizzato".
Ciò vuol dire (non)/sincronizzato con altre porzioni di codice.
La definizione da dizionario invece differisce.
Per Treccani: "Sincrono: Che avviene nello stesso momento",
mentre sappiamo che un'operazione sincrona rispetto ad un'altra
non avviene allo stesso tempo.