承诺中的 if-else 流程(蓝鸟) [英] if-else flow in promise (bluebird)
本文介绍了承诺中的 if-else 流程(蓝鸟)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码的简短版本.
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();
}
两个条件都调用functionA
.有没有办法避免其他情况?我可以做 fs.writeFileSync
但我正在寻找一个非阻塞的解决方案.
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.
推荐答案
我想你在找
(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();
});
这篇关于承诺中的 if-else 流程(蓝鸟)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文