From 6f7bf89604208a435c0bb574d845b878e38f4fb1 Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Tue, 9 Jul 2024 10:00:23 +0200 Subject: [PATCH] update --- async-await/0_callbacks.js | 3 +++ async-await/1_promises.js | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/async-await/0_callbacks.js b/async-await/0_callbacks.js index 624ddfb..9bfa8e6 100644 --- a/async-await/0_callbacks.js +++ b/async-await/0_callbacks.js @@ -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. diff --git a/async-await/1_promises.js b/async-await/1_promises.js index 7a0a960..036ba0d 100644 --- a/async-await/1_promises.js +++ b/async-await/1_promises.js @@ -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),