4 ms·
I've adapted the node-fibers library to write synchronous style code. It works really well for my needs, but I do understand that my approach does litter the f
by martin-adams 11y ago
I've adapted the node-fibers library to write synchronous style code. It works really well for my needs, but I do understand that my approach does litter the function prototype which is not ideal.
Code looks like this:
var sync = require('./sync');
sync(function () {
try {
var result = someAsyncFunc1.callSync(this, 'param1', param2');
if (result.something === true) {
var result2 = someAsyncFunc2.callSync(this, 'param1');
} else {
var result2 = someAsyncFunc3.callSync(this, 'param1');
}
console.log(result2.message);
} catch (ex) {
// One of them returned an err param in their callback
}
});
I haven't tested the performance, so no idea if it's a running like a dog.
- btown 11y agoMeteor does something similar with Fibers, but you wrap each function ahead of time, so you don't need to worry about littering Function.prototype. https://www.discovermeteor.com/blog/wrapping-npm-packages/ https://www.discovermeteor.com/blog/wrapping-npm-packages/ It feels a bit like "get that sync code off my Javascript lawn!" but once you get used to it, it's pretty great in practice, and good for introducing newcomers.