The background
I'm writing my next server project in node. The previous version was written in language D, which is majorly an synchronized implementation. We all know, that JS uses a lot of async This is good for performance, but hard to maintain (at least for me). Because node and its node modules might use different async approach (callback, promise and await/async), which makes the code looks not in a consistent style. I'm looking for a balanced approach, that not only can create a high quality and performance node application, but fit the common programming style.
The problem
After first round prototyping, I have my first server. When I start the server, it will first read configuration and then load a set of modules. I expect them to be loaded sequentially. But as I mentioned above, node modules might work in async way. I expect module b to be loaded exactly when module a is loaded, I must use Promise
pattern. I have to write code in crazy way, although Promise
is already a very programmer friendly approach.
No, I go to the other direction. Currently I choose deasync
, which is a gyp module for node. It can really blocks the code execution like this.
const deasync = require('deasync');
let done = false;
loadModule1Async((err, result) => {
done = true;
});
deasync.loopWhile(() => !done);
done = false;
loadModule2Async((err, result) => {
done = true;
});
deasync.loopWhile(() => !done);
OR even more magical like this
const deasync = require('deasync');
const loadModule1Sync = deasync(loadModule1Async);
const loadModule2Sync = deasync(loadModule2Async);
try {
loadModule1Sync();
loadModule2Sync();
} catch(ex) {
console.error(ex);
}
IMO, the deasync
way is more straightforward to me, but I have two concerns:
- It need to be compiled against the target platform, so that the deployment process will be complicated for production.
- I checked its issue list. There are some known serious issue, e.g. hanging in some extream cases.
The question
Can the coming new feature async/await
really do the same thing as deasync
does? If it is never gonna happen due to the JS nature. I might stop thinking about this.
Please share me some lights on the way to node.