Possible Duplicate:
Synchronous database queries with Node.js
Usually, we execute a SQL query and get the result in a callback. like this:
sqlExec('SELECT COL FROM TBL;', function (err, results, fields) {
doSomething(results);
});
But if we need to do more complicated job with the SQL results, the code will be more uglier, like this:
var tmp = '';
sqlExec('SELECT COL1 FROM TBL1;', function (err, results, fields) {
tmp = doSomething(results);
sqlExec('SELECT COL2 FROM TBL2 WHERE CONDITION2 = ' + tmp + ';', function (err, results, fields) {
tmp = doSomething2(results);
sqlExec('SELECT COL3 FROM TBL3 WHERE CONDITION3 = ' + tmp + ';', function (err, results, fields) {
....
});
});
});
Do we have a idea to make it sync? like this:
var tmp = '', result = '';
result = sqlExec('SELECT COL1 FROM TBL1;');
tmp = doSomething(result);
sqlExec('SELECT COL2 FROM TBL2 WHERE CONDITION2 = ' + tmp + ';');
tmp = doSomething(result);
sqlExec('SELECT COL3 FROM TBL3 WHERE CONDITION3 = ' + tmp + ';');
...
Thanks, Gcaufy