diff --git a/async-await/1_promises.js b/async-await/1_promises.js index 0a466dc..7a0a960 100644 --- a/async-await/1_promises.js +++ b/async-await/1_promises.js @@ -10,43 +10,49 @@ A Promise can be in one of these three states: - Pending (the operation is being processed) - - Fullfilled (the operation has completed successfully) - - Rejected + - Fullfilled (the operation has completed successfully, resolve has been called) + - Rejected (the operation has not completed successfully, reject has been called) */ const promise = new Promise(function (resolve, reject) { - setTimeout(() => resolve('done'), 500); - }); + setTimeout(() => resolve('done'), 5000); +}); - /* - 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! - */ - promise.then( - result => console.log('The operation was successful. It returned ' + result), - error => console.log('The operation was not successful: ' + error) - ); +/* + 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! +*/ +promise.then( + result => console.log('The operation was successful. It returned ' + result), + error => console.log('The operation was not successful: ' + error) +); - /* - Or we can pass only one argument if we're interested only in a positive result - */ - promise.then( - result => console.log('The operation was successful. It returned ' + result) - ); +/* + Or we can pass only one argument if we're interested only in a positive result +*/ +promise.then( + result => console.log('The operation was successful. It returned ' + result) +); - /* - Or we can pass only one argument to the method "catch" if we're interested - in negative results only. +/* + Or we can pass only one argument to the method "catch" if we're interested + in negative results only. - promise.catch internally just calls promise.then(null, f) - */ - promise.catch( - error => console.log(error) - ); + promise.catch internally just calls promise.then(null, f) +*/ +promise.catch( + error => console.log(error) +); - /* - finally gets always called - */ - promise.finally( - () => console.log('The execution has terminated. Bye') - ); \ No newline at end of file +/* + finally gets always called +*/ +promise.finally( + () => console.log('The execution has terminated. Bye') +); + +/* + This line is used to demonstrate that the code within "then, catch, etc." + is in the event loop, as this is the first line getting executed. +*/ +console.log("Last line"); \ No newline at end of file diff --git a/async-await/3_promise-chaining.js b/async-await/2_promise-chaining.js similarity index 100% rename from async-await/3_promise-chaining.js rename to async-await/2_promise-chaining.js diff --git a/async-await/event-loop.txt b/async-await/event-loop.txt index 4b54bc2..e72618e 100644 --- a/async-await/event-loop.txt +++ b/async-await/event-loop.txt @@ -1,3 +1,4 @@ -JavaScript is single-threaded (but note that NodeJS is not entirely single-threaded, as it internally mantains a thread pool), but it can still take advantage of asynchronous programming. +JavaScript is single-threaded (but note that NodeJS is not entirely single-threaded, as it internally mantains a thread +pool), but it can still take advantage of asynchronous programming. -The Event Loop, which is a JavaScript construct that completes a new task while waiting for another \ No newline at end of file +The Event Loop, which is a JavaScript construct that completes a new task while waiting for another. \ No newline at end of file