From f7ec5c3f32db910d5ed7d0964481d23da9d8be25 Mon Sep 17 00:00:00 2001 From: xfarrow Date: Wed, 11 Oct 2023 10:19:15 +0200 Subject: [PATCH] Update --- .../{callbacks.js => 0_callbacks.js} | 2 +- .../tutorials/{promises.js => 1_promises.js} | 0 ...mise_chaining.js => 2_promise_chaining.js} | 0 .../{async-await.js => 3_async-await.js} | 22 +++++++++++++ backend/tutorials/othertests.js | 11 ------- backend/tutorials/sincrono_vs_asincrono.txt | 31 +++++++++++++++++++ 6 files changed, 54 insertions(+), 12 deletions(-) rename backend/tutorials/{callbacks.js => 0_callbacks.js} (93%) rename backend/tutorials/{promises.js => 1_promises.js} (100%) rename backend/tutorials/{promise_chaining.js => 2_promise_chaining.js} (100%) rename backend/tutorials/{async-await.js => 3_async-await.js} (63%) delete mode 100644 backend/tutorials/othertests.js create mode 100644 backend/tutorials/sincrono_vs_asincrono.txt diff --git a/backend/tutorials/callbacks.js b/backend/tutorials/0_callbacks.js similarity index 93% rename from backend/tutorials/callbacks.js rename to backend/tutorials/0_callbacks.js index c430ade..bab79b5 100644 --- a/backend/tutorials/callbacks.js +++ b/backend/tutorials/0_callbacks.js @@ -34,7 +34,7 @@ function entryPoint(){ valore, in questo caso console.log("I started here!"); Questo però è solo un esempio in quanto le istruzioni verranno - eseguite in maniera sincrona (non concorrenti), ma che ci permette di + eseguite in maniera sincrona, ma che ci permette di comprendere le basi di questo meccanismo. */ diff --git a/backend/tutorials/promises.js b/backend/tutorials/1_promises.js similarity index 100% rename from backend/tutorials/promises.js rename to backend/tutorials/1_promises.js diff --git a/backend/tutorials/promise_chaining.js b/backend/tutorials/2_promise_chaining.js similarity index 100% rename from backend/tutorials/promise_chaining.js rename to backend/tutorials/2_promise_chaining.js diff --git a/backend/tutorials/async-await.js b/backend/tutorials/3_async-await.js similarity index 63% rename from backend/tutorials/async-await.js rename to backend/tutorials/3_async-await.js index bcdf7a9..4568076 100644 --- a/backend/tutorials/async-await.js +++ b/backend/tutorials/3_async-await.js @@ -26,6 +26,25 @@ async function f2() { 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, il flusso di esecuzione va fuori al blocco di codide. 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'); + + // Il risultato sarà: + // Start, Before Await, End, After await + // Domande // // Why await only works in async function in javascript? @@ -37,3 +56,6 @@ async function f2() { // 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 diff --git a/backend/tutorials/othertests.js b/backend/tutorials/othertests.js deleted file mode 100644 index 2c08dc4..0000000 --- a/backend/tutorials/othertests.js +++ /dev/null @@ -1,11 +0,0 @@ -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'); \ No newline at end of file diff --git a/backend/tutorials/sincrono_vs_asincrono.txt b/backend/tutorials/sincrono_vs_asincrono.txt new file mode 100644 index 0000000..12f238f --- /dev/null +++ b/backend/tutorials/sincrono_vs_asincrono.txt @@ -0,0 +1,31 @@ +Link utili: +https://stackoverflow.com/questions/748175/asynchronous-vs-synchronous-execution-what-is-the-difference +https://stackoverflow.com/questions/7131991/asynchronous-and-synchronous-terms + +La parola "sincrono" in contesto informatico vuol dire "sincronizzato", ovvero +il chiamante deve aspettare la risposta del chiamato, mentre +"async" vuol dire "non sincronizzato". +Ciò vuol dire (non)/sincronizzato con altre porzioni di codice. +La definizione da dizionario invece differisce. +Per Treccani: "Sincrono: Che avviene nello stesso momento", +mentre sappiamo che un'operazione sincrona rispetto ad un'altra +non avviene allo stesso tempo. +In informatica dire "un metodo è (a)sincrono" deve +sempre accompagnate da "rispetto a chi" è (a)sincrono. + +Possiamo anche pensarla così: (https://stackoverflow.com/a/32052611/18371893) + +In a nutshell, synchronization refers to two or more processes' start and end points, NOT their executions. +In this example, Process A's endpoint is synchronized with Process B's start point: + +SYNCHRONOUS + |--------A--------| + |--------B--------| + +Asynchronous processes, on the other hand, do not have their start and endpoints synchronized: + +ASYNCHRONOUS + |--------A--------| + |--------B--------| + +Where Process A overlaps Process B, they're running concurrently or synchronously (dictionary definition), hence the confusion. \ No newline at end of file