This commit is contained in:
Alessandro Ferro 2024-07-09 10:00:23 +02:00
parent 04e200e406
commit 6f7bf89604
2 changed files with 8 additions and 1 deletions

View File

@ -46,6 +46,9 @@ function entryPoint () {
asynchronous function, such as time, such as
setTimeout(callback, delay).
Callbacks are not asynchronous by nature, but can be used
for asynchronous purposes.
The problem is that this makes the code harder to read
so modern JS is written using Promises and async/await
constructs.

View File

@ -12,6 +12,10 @@
- Pending (the operation is being processed)
- Fullfilled (the operation has completed successfully, resolve has been called)
- Rejected (the operation has not completed successfully, reject has been called)
A promise represents the completion of a (likely) asynchronous function. It is an object
that might return a value in the future. It accomplishes the same basic goal as a
callback function, but with many additional features and a more readable syntax.
*/
const promise = new Promise(function (resolve, reject) {
setTimeout(() => resolve('done'), 5000);
@ -20,7 +24,7 @@ const promise = new Promise(function (resolve, reject) {
/*
The first argument of .then is a function that runs when the promise is resolved and receives the result.
The second argument of .then is a function that runs when the promise is rejected and receives the error.
The function passed to "then()" is put in the event loop queue!
The function passed to "then()" is put in the queue!
*/
promise.then(
result => console.log('The operation was successful. It returned ' + result),