This is a short version of my code.
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require("fs"));
if (conditionA) {
fs.writeFileAsync(file, jsonData).then(function() {
return functionA();
});
} else {
functionA();
}
Both conditions call functionA
. Is there way to avoid else condition? I can do fs.writeFileSync
but I am looking for a non-blocking solution.
While other suggestions here work, personally I prefer the following.
It has the disadvantage of always creating this additional promise (rather minor IMO) but looks much cleaner to my eye. You can also add other conditions/logic easily inside the IIFE.
EDIT
After implementing things like this for a long time now I have definitely changed to something slightly clearer. The initial promise is created regardless so it is much clearer to simply do:
inside of the first .then() function.
You could always use
Promise.all()
with conditional functionOr, if you want
functionA
only called after the file maybe was written you can separate:I think you're looking for
which is short for