Skip to content

Node.js change function async to sync

Almost Node.js‘s libraries are structured asynchronous.

But some app want to returning synchronous.

Below link maybe help you to change function async to sync.

http://alexeypetrushin.github.io/synchronize/docs/index.html


// change to async to sync
var sync = require('synchronize');
sync.fiber(...);
sync(conn, 'query');// conn means mysql connector here.


// original(async)
conn.query([SOME QUERY], function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});


// new(sync)
try{
var rows = conn.query([SOME QUERY]);
console.log('The solution is: ', rows[0].solution);
}catch (err){
throw err;
}