这是我的代码的简短版本。
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require("fs"));
if (conditionA) {
fs.writeFileAsync(file, jsonData).then(function() {
return functionA();
});
} else {
functionA();
}
两个条件都调用functionA
。有没有办法避免其他情况?我可以做 fs.writeFileSync
但我正在寻找一个非阻塞的解决方案。
最佳答案
我想你正在寻找
(conditionA
? fs.writeFileAsync(file, jsonData)
: Promise.resolve())
.then(functionA);
简称
var waitFor;
if (conditionA)
waitFor = fs.writeFileAsync(file, jsonData);
else
waitFor = Promise.resolve(undefined); // wait for nothing,
// create fulfilled promise
waitFor.then(function() {
return functionA();
});
关于node.js - promise 中的 if-else 流程( Bluebird ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26599798/