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,31 +1,30 @@
// https://javascript.info/callbacks
entryPoint();
entryPoint()
function entryPoint () {
execute_action('something', function (error, time_of_completion) {
if (error) {
console.log('Something happened');
console.log('Something happened')
} else {
console.log('Time of completion: ' + new Date(time_of_completion).toDateString());
}
});
console.log("I don't need execute_action's value");
console.log('Time of completion: ' + new Date(time_of_completion).toDateString())
}
})
console.log("I don't need execute_action's value")
}
function execute_action (param, callback) {
function execute_action (param, callback) {
if (param == 'something') {
console.log('Executing action: ' + param);
callback(null, Date.now());
console.log('Executing action: ' + param)
callback(null, Date.now())
} else {
// We can call callback with one argument even if
// the signature states two parameters.
callback(new Error('Invalid parameter'));
}
callback(new Error('Invalid parameter'))
}
}
/*
/*
This is useful when, for example, execute_action performs slow operations
(such as I/O, HTTP requests etc) and we need its result to continue a
specific operation (in this case the date of completion), without blocking
@ -54,4 +53,3 @@ function entryPoint () {
constructs.
*/

View File

@ -18,8 +18,8 @@
callback function, but with many additional features and a more readable syntax.
*/
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.
@ -29,14 +29,14 @@ const promise = new Promise(function (resolve, reject) {
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 to the method "catch" if we're interested
@ -46,17 +46,17 @@ promise.then(
*/
promise.catch(
error => console.log(error)
);
)
/*
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");
console.log('Last line')

View File

@ -4,26 +4,26 @@
// internally calling resolve().
new Promise(function (resolve, reject) {
setTimeout(() => resolve(1), 1);
}).then(function (result) {
console.log(result);
return result * 2;
}).then(function (result) {
console.log(result);
return result * 2;
}).then(function (result) {
console.log(result);
return result * 2;
});
setTimeout(() => resolve(1), 1)
}).then(function (result) {
console.log(result)
return result * 2
}).then(function (result) {
console.log(result)
return result * 2
}).then(function (result) {
console.log(result)
return result * 2
})
/*
/*
It will print
1
2
4
*/
/*
/*
It means that "then" is internally implemented roughly as follows:
function then(f){
@ -33,16 +33,16 @@ new Promise(function (resolve, reject) {
}
*/
// Another example:
// Another example:
fetch('http://www.fsf.org')
// .then below runs when the remote server responds
fetch('http://www.fsf.org')
// .then below runs when the remote server responds
.then(function (response) {
// response.text() returns a new promise that resolves with the full response text
// when it loads
return response.text();
return response.text()
})
.then(function (text) {
// ...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 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.
// 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.
async function f2 () {
const promise = new Promise((resolve, reject) => {
setTimeout(() => resolve('done!'), 1000);
});
const result = await promise; // wait until the promise resolves (*)
console.log(result); // "done!"
setTimeout(() => resolve('done!'), 1000)
})
const result = await promise // wait until the promise resolves (*)
console.log(result) // "done!"
}
f2();
f2()
// The code in the same function after "await"
// 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
// consider the following example:
async function exampleAsyncFunction () {
console.log('Before await');
console.log('Before await')
await new Promise(function (resolve, reject) {
setTimeout(() => resolve('done'), 500);
}); // Pauses execution here until the promise resolves.
console.log('After await');
setTimeout(() => resolve('done'), 500)
}) // Pauses execution here until the promise resolves.
console.log('After await')
}
console.log('Start');
exampleAsyncFunction();
console.log('End');
console.log('Start')
exampleAsyncFunction()
console.log('End')
// The result will be:
// Start, Before Await, End, After await