npx eslint --fix

This commit is contained in:
Alessandro Ferro
2024-02-23 11:17:02 +01:00
parent 4a712f4de3
commit 7bd6889768
18 changed files with 618 additions and 662 deletions

View File

@ -1,35 +1,32 @@
// https://javascript.info/callbacks
function execute_action(param, callback){
if(param == "something"){
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"))
}
function execute_action (param, callback) {
if (param == 'something') {
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'));
}
}
function entryPoint(){
/* ===== Begin Simple callback ===== */
function entryPoint () {
/* ===== Begin Simple callback ===== */
execute_action("something", function (error, time_of_completion){
if(error){
console.log("Something happened");
}
else{
console.log("Time of completion: " + new Date(time_of_completion).toDateString());
}
});
console.log("I started here!");
/*
Ciò è utile se ad esempio execute_action fa operazioni lente (ad esempio
execute_action('something', function (error, time_of_completion) {
if (error) {
console.log('Something happened');
} else {
console.log('Time of completion: ' + new Date(time_of_completion).toDateString());
}
});
console.log('I started here!');
/*
Ciò è utile se ad esempio execute_action fa operazioni lente (ad esempio
scrittura su database, connessioni HTTP ecc..) ma abbiamo bisogno
del suo valore di ritorno per continuare una determinata operazione
(in questo caso la data di completamento dell'esecuzione),
(in questo caso la data di completamento dell'esecuzione),
senza però bloccare le altre operazioni che non hanno bisogno di tale
valore, in questo caso console.log("I started here!");
@ -37,12 +34,12 @@ function entryPoint(){
eseguite in maniera sincrona, ma che ci permette di
comprendere le basi di questo meccanismo.
*/
/* ===== End Simple Callback ===== */
/* ===== End Simple Callback ===== */
}
entryPoint();
// callbacks usually indicate that the
// execution of their code will continue
// asynchronously.
// asynchronously.

View File

@ -10,24 +10,24 @@
Remember that Promises are not intrensically asyncronous
*/
let promise = new Promise(function(resolve, reject) {
setTimeout(() => resolve("done"), 500);
});
const promise = new Promise(function (resolve, reject) {
setTimeout(() => resolve('done'), 500);
});
/*
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.
*/
promise.then(
result => console.log("The operation was successful. It returned " + result),
error => console.log("The operation was not successful: " + error)
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)
result => console.log('The operation was successful. It returned ' + result)
);
/*
@ -37,12 +37,12 @@ promise.then(
promise.catch internally just calls promise.then(null, f)
*/
promise.catch(
error => console.log(error)
error => console.log(error)
);
/*
finally gets always called
*/
promise.finally(
() => console.log("The execution has terminated. Bye")
);
() => console.log('The execution has terminated. Bye')
);

View File

@ -3,29 +3,21 @@
// .then() returns a new Promise when you do "return",
// 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;
});
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;
});
/*
It will print
It will print
1
2
4
@ -33,7 +25,7 @@ new Promise(function(resolve, reject) {
/*
It means that "then" is internally implemented roughly as follows:
function then(f){
return new Promise(function(resolve, reject) {
resolve(f());
@ -45,12 +37,12 @@ new Promise(function(resolve, reject) {
fetch('http://www.fsf.org')
// .then below runs when the remote server responds
.then(function(response) {
.then(function (response) {
// response.text() returns a new promise that resolves with the full response text
// when it loads
return response.text();
})
.then(function(text) {
.then(function (text) {
// ...and here's the content of the remote file
console.log(text);
});
});

View File

@ -3,63 +3,62 @@
// A async function always returns a promise. Other values are wrapped in a resolved promise automatically.
// Async e Await sono solo "sintassi zuccherina" per rendere l'utilizzo delle Promise più semplice
async function f1() {
return 1;
}
f1().then(console.log); // 1
async function f1 () {
return 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
// Lets emphasize: await literally suspends the function execution until the promise settles,
// Lets emphasize: await literally suspends the function execution until the promise settles,
// and then resumes it with the promise result.
async function f2() {
let promise = new Promise((resolve, reject) => {
setTimeout(() => resolve("done!"), 1000)
async function f2 () {
const promise = new Promise((resolve, reject) => {
setTimeout(() => resolve('done!'), 1000);
});
let result = await promise; // wait until the promise resolves (*)
const result = await promise; // wait until the promise resolves (*)
console.log(result); // "done!"
}
f2();
// Tutto il codice nella stessa funzione (o nello stesso blocco di codice)
// dopo "await" è da considerarsi nel "then()" di una promessa. Pertanto dopo
// await (ma prima del completamento della Promise),
// il flusso di esecuzione va fuori a quel blocco di codice. Ad esempio considera
// il seguente esempio:
async function exampleAsyncFunction() {
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');
}
console.log('Start');
exampleAsyncFunction();
console.log('End');
f2();
// Il risultato sarà:
// Start, Before Await, End, After await
// Viene prima "End" e poi "After Await", perché
// dopo await, il flusso di esecuzione ritorna al
// chiamante
// Tutto il codice nella stessa funzione (o nello stesso blocco di codice)
// dopo "await" è da considerarsi nel "then()" di una promessa. Pertanto dopo
// await (ma prima del completamento della Promise),
// il flusso di esecuzione va fuori a quel blocco di codice. Ad esempio considera
// il seguente esempio:
async function exampleAsyncFunction () {
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');
}
// Domande
//
// Why await only works in async function in javascript?
// https://stackoverflow.com/questions/49640647/why-await-only-works-in-async-function-in-javascript
//
// Why using async-await
// https://stackoverflow.com/questions/42624647/why-use-async-when-i-have-to-use-await
//
// Si faccia presente che non è possibile creare da zero una funzione asincrona (come
// setInterval o fetch)
// https://stackoverflow.com/questions/61857274/how-to-create-custom-asynchronous-function-in-javascript
//
// Altra domanda interessante
// https://stackoverflow.com/questions/42624647/why-use-async-when-i-have-to-use-await
console.log('Start');
exampleAsyncFunction();
console.log('End');
// Il risultato sarà:
// Start, Before Await, End, After await
// Viene prima "End" e poi "After Await", perché
// dopo await, il flusso di esecuzione ritorna al
// chiamante
// Domande
//
// Why await only works in async function in javascript?
// https://stackoverflow.com/questions/49640647/why-await-only-works-in-async-function-in-javascript
//
// Why using async-await
// https://stackoverflow.com/questions/42624647/why-use-async-when-i-have-to-use-await
//
// Si faccia presente che non è possibile creare da zero una funzione asincrona (come
// setInterval o fetch)
// https://stackoverflow.com/questions/61857274/how-to-create-custom-asynchronous-function-in-javascript
//
// Altra domanda interessante
// https://stackoverflow.com/questions/42624647/why-use-async-when-i-have-to-use-await

View File

@ -3,10 +3,10 @@
The function delay(ms) should return a promise. That promise should resolve after ms milliseconds, so that we can add .then to it, like this:
*/
function delay(ms){
return new Promise(resolve => {
setTimeout(resolve, ms);
});
function delay (ms) {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}
delay(1000).then(() => console.log("Hello world!"));
delay(1000).then(() => console.log('Hello world!'));