diff --git a/async-await/0_callbacks.js b/async-await/0_callbacks.js index e7e4a4c..624ddfb 100644 --- a/async-await/0_callbacks.js +++ b/async-await/0_callbacks.js @@ -40,6 +40,15 @@ function entryPoint () { Executing action: something Time of completion: Sun Jun 30 2024 I don't need execute_action's value + + 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 + setTimeout(callback, delay). + + The problem is that this makes the code harder to read + so modern JS is written using Promises and async/await + constructs. */ \ No newline at end of file diff --git a/async-await/1_promises.js b/async-await/1_promises.js index d3478c0..0a466dc 100644 --- a/async-await/1_promises.js +++ b/async-await/1_promises.js @@ -1,12 +1,17 @@ // https://javascript.info/promise-basics /* - The function passed to Promise is called "executor". - and gets executed the moment the Promise is created. + The function passed to Promise is called "executor" + and gets executed synchronously the moment the Promise is created. When the Promise ends, the callback function should either call the "resolve" or "reject" callbacks: resolve(value) — if the job is finished successfully, with result value. reject(error) — if an error has occurred, error is the error object. + + A Promise can be in one of these three states: + - Pending (the operation is being processed) + - Fullfilled (the operation has completed successfully) + - Rejected */ const promise = new Promise(function (resolve, reject) { setTimeout(() => resolve('done'), 500); diff --git a/async-await/event-loop.txt b/async-await/event-loop.txt new file mode 100644 index 0000000..4b54bc2 --- /dev/null +++ b/async-await/event-loop.txt @@ -0,0 +1,3 @@ +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