intentation

This commit is contained in:
xfarrow 2024-08-06 16:02:04 +02:00
parent 0e85666f23
commit fa46b1fce5
4 changed files with 82 additions and 84 deletions

View File

@ -1,27 +1,26 @@
// https://javascript.info/callbacks // https://javascript.info/callbacks
entryPoint(); entryPoint()
function entryPoint () { function entryPoint () {
execute_action('something', function (error, time_of_completion) { execute_action('something', function (error, time_of_completion) {
if (error) { if (error) {
console.log('Something happened'); console.log('Something happened')
} else { } else {
console.log('Time of completion: ' + new Date(time_of_completion).toDateString()); console.log('Time of completion: ' + new Date(time_of_completion).toDateString())
} }
}); })
console.log("I don't need execute_action's value"); console.log("I don't need execute_action's value")
} }
function execute_action (param, callback) { function execute_action (param, callback) {
if (param == 'something') { if (param == 'something') {
console.log('Executing action: ' + param); console.log('Executing action: ' + param)
callback(null, Date.now()); callback(null, Date.now())
} else { } else {
// We can call callback with one argument even if // We can call callback with one argument even if
// the signature states two parameters. // the signature states two parameters.
callback(new Error('Invalid parameter')); callback(new Error('Invalid parameter'))
} }
} }
@ -54,4 +53,3 @@ function entryPoint () {
constructs. constructs.
*/ */

View File

@ -18,8 +18,8 @@
callback function, but with many additional features and a more readable syntax. callback function, but with many additional features and a more readable syntax.
*/ */
const promise = new Promise(function (resolve, reject) { const promise = new Promise(function (resolve, reject) {
setTimeout(() => resolve('done'), 5000); setTimeout(() => resolve('done'), 5000)
}); })
/* /*
The first argument of .then is a function that runs when the promise is resolved and receives the result. The first argument of .then is a function that runs when the promise is resolved and receives the result.
@ -29,14 +29,14 @@ const promise = new Promise(function (resolve, reject) {
promise.then( promise.then(
result => console.log('The operation was successful. It returned ' + result), result => console.log('The operation was successful. It returned ' + result),
error => console.log('The operation was not successful: ' + error) 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 Or we can pass only one argument if we're interested only in a positive result
*/ */
promise.then( promise.then(
result => console.log('The operation was successful. It returned ' + result) 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 Or we can pass only one argument to the method "catch" if we're interested
@ -46,17 +46,17 @@ promise.then(
*/ */
promise.catch( promise.catch(
error => console.log(error) error => console.log(error)
); )
/* /*
finally gets always called finally gets always called
*/ */
promise.finally( promise.finally(
() => console.log('The execution has terminated. Bye') () => console.log('The execution has terminated. Bye')
); )
/* /*
This line is used to demonstrate that the code within "then, catch, etc." 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. is in the event loop, as this is the first line getting executed.
*/ */
console.log("Last line"); console.log('Last line')

View File

@ -4,17 +4,17 @@
// internally calling resolve(). // internally calling resolve().
new Promise(function (resolve, reject) { new Promise(function (resolve, reject) {
setTimeout(() => resolve(1), 1); setTimeout(() => resolve(1), 1)
}).then(function (result) { }).then(function (result) {
console.log(result); console.log(result)
return result * 2; return result * 2
}).then(function (result) { }).then(function (result) {
console.log(result); console.log(result)
return result * 2; return result * 2
}).then(function (result) { }).then(function (result) {
console.log(result); console.log(result)
return result * 2; return result * 2
}); })
/* /*
It will print It will print
@ -40,9 +40,9 @@ new Promise(function (resolve, reject) {
.then(function (response) { .then(function (response) {
// response.text() returns a new promise that resolves with the full response text // response.text() returns a new promise that resolves with the full response text
// when it loads // when it loads
return response.text(); return response.text()
}) })
.then(function (text) { .then(function (text) {
// ...and here's the content of the remote file // ...and here's the content of the remote file
console.log(text); console.log(text)
}); })

View File

@ -4,10 +4,10 @@
// Async and await are merely syntactic sugar in order to make Promise usage easier // Async and await are merely syntactic sugar in order to make Promise usage easier
async function f1 () { async function f1 () {
return 1; return 1
} }
f1().then(console.log); // 1 f1().then(console.log) // 1
// The keyword await makes JavaScript wait until that promise settles and returns its result. // The keyword await makes JavaScript wait until that promise settles and returns its result.
// It can be used in async functions only. // It can be used in async functions only.
@ -15,12 +15,12 @@ f1().then(console.log); // 1
// and then resumes it with the promise result. // and then resumes it with the promise result.
async function f2 () { async function f2 () {
const promise = new Promise((resolve, reject) => { const promise = new Promise((resolve, reject) => {
setTimeout(() => resolve('done!'), 1000); setTimeout(() => resolve('done!'), 1000)
}); })
const result = await promise; // wait until the promise resolves (*) const result = await promise // wait until the promise resolves (*)
console.log(result); // "done!" console.log(result) // "done!"
} }
f2(); f2()
// The code in the same function after "await" // The code in the same function after "await"
// is to be intended in the "then()" of the primise. This means // is to be intended in the "then()" of the primise. This means
@ -28,15 +28,15 @@ f2();
// the flow of execution goes out that code block. For example // the flow of execution goes out that code block. For example
// consider the following example: // consider the following example:
async function exampleAsyncFunction () { async function exampleAsyncFunction () {
console.log('Before await'); console.log('Before await')
await new Promise(function (resolve, reject) { await new Promise(function (resolve, reject) {
setTimeout(() => resolve('done'), 500); setTimeout(() => resolve('done'), 500)
}); // Pauses execution here until the promise resolves. }) // Pauses execution here until the promise resolves.
console.log('After await'); console.log('After await')
} }
console.log('Start'); console.log('Start')
exampleAsyncFunction(); exampleAsyncFunction()
console.log('End'); console.log('End')
// The result will be: // The result will be:
// Start, Before Await, End, After await // Start, Before Await, End, After await