Salut !
Je me demande, en général, comment identifie t'on si l'appel d'une fonction d'un module se fait de manière synchrone ou asynchrone ?
Salut !
Je me demande, en général, comment identifie t'on si l'appel d'une fonction d'un module se fait de manière synchrone ou asynchrone ?
Bonjour,
Ce lien devrait répondre à tes questions![]()
merci arthur57 !!! je commence à voir plus clair !!!
Bon je rame un peu.
je n'ai pas de garantie d'avoir le résultat de ma requete à la fin de ma fonction... donc je génère un événement ? ou alors je place des await ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 function check_login(json_, socket_) { let con = mysql.createConnection(options); con.on('error', function(error_) { console.error("connection error ", error_); }); con.connect(function(error_) { if(error_) console.log("connect error : ", error_); }); let query = "SELECT count(*) as num FROM user"; console.log("query : ", query); con.query(query, function(error_, result_, fields_) { if(error_) { console.error("query error ", error_.code); return; } const count = result_[0].num; }); con.end(); };![]()
Je ne connais pas cette API mais de ce que je comprends du code et de comment il s'organise tu as de grandes chances de fermer ta connexion avant même d'avoir reçu le résultat de ta requête.
J'ai peut être mal saisi le rôle de cette fonction end()Terminating connections
There are two ways to end a connection. Terminating a connection gracefully is done by calling the end() method:
connection.end(function(err) {
// The connection is terminated now
});
This will make sure all previously enqueued queries are still before sending a COM_QUIT packet to the MySQL server. If a fatal error occurs before the COM_QUIT packet can be sent, an err argument will be provided to the callback, but the connection will be terminated regardless of that.
source : https://github.com/mysqljs/mysql#ter...ng-connections
Every method you invoke on a connection is queued and executed in sequence.
Closing the connection is done using end() which makes sure all remaining queries are executed before sending a quit packet to the mysql server.
source : https://github.com/mysqljs/mysql#introduction
Partager