This commit is contained in:
xfarrow 2024-07-01 10:52:25 +02:00
parent 775dbd3b6f
commit f4fa085d30
3 changed files with 19 additions and 2 deletions

View File

@ -41,5 +41,14 @@ function entryPoint () {
Time of completion: Sun Jun 30 2024 Time of completion: Sun Jun 30 2024
I don't need execute_action's value 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.
*/ */

View File

@ -1,12 +1,17 @@
// https://javascript.info/promise-basics // https://javascript.info/promise-basics
/* /*
The function passed to Promise is called "executor". The function passed to Promise is called "executor"
and gets executed the moment the Promise is created. and gets executed synchronously the moment the Promise is created.
When the Promise ends, the callback function should When the Promise ends, the callback function should
either call the "resolve" or "reject" callbacks: either call the "resolve" or "reject" callbacks:
resolve(value) if the job is finished successfully, with result value. resolve(value) if the job is finished successfully, with result value.
reject(error) if an error has occurred, error is the error object. 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) { const promise = new Promise(function (resolve, reject) {
setTimeout(() => resolve('done'), 500); setTimeout(() => resolve('done'), 500);

View File

@ -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