node.js if-else 流在承诺中(蓝鸟)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26599798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:02:53  来源:igfitidea点击:

if-else flow in promise (bluebird)

node.jspromisebluebird

提问by vinayr

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.writeFileSyncbut I am looking for a non-blocking solution.

两个条件都调用functionA。有没有办法避免其他条件?我可以,fs.writeFileSync但我正在寻找非阻塞解决方案。

回答by Bergi

I think you're looking for

我想你正在寻找

(conditionA 
  ? fs.writeFileAsync(file, jsonData)
  : Promise.resolve())
.then(functionA);

which is short for

这是缩写

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();
});

回答by Goblinlord

While other suggestions here work, personally I prefer the following.

虽然这里的其他建议有效,但我个人更喜欢以下建议。

Promise.resolve(function(){
  if (condition) return fs.writeFileAsync(file, jsonData);
}())
.then()

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.

它的缺点是总是创建这个额外的承诺(相当小的 IMO),但在我看来看起来更干净。您还可以在 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:

在实施这样的事情很长一段时间之后,我肯定已经改变了一些更清晰的东西。无论如何都会创建初始承诺,因此简单地执行以下操作会更清晰:

/* Example setup */

var someCondition = (Math.random()*2)|0;
var value = "Not from a promise";
var somePromise = new Promise((resolve) => setTimeout(() => resolve('Promise value'), 3000));


/* Example */

Promise.resolve()
.then(() => {
  if (someCondition) return value;
  return somePromise;
})
.then((result) => document.body.innerHTML = result);
Initial state
实际上,在你的情况下,它只是

if (someCondition) return somePromise;

inside of the first .then() function.

在第一个 .then() 函数内部。

回答by aarosil

You could always use Promise.all()with conditional function

你总是可以使用Promise.all()条件函数

var condition = ...;

var maybeWrite = function(condition, file, jsonData){
    return (condition) ? fs.writeFileAsync(file, jsonData) : Promise.resolve(true);
}

Promise.all([maybeWrite(condition, file, jsonData),functionA()])
.then(function(){
    // here 'functionA' was called, 'writeFileAsync' was maybe called
})

Or, if you want functionAonly called after the file maybe was written you can separate:

或者,如果您只想functionA在文件写入后调用,您可以分开:

maybeWrite(condition, file, jsonData)
.then(function(){
    // here file may have been written, you can call 'functionA'
    return functionA();
})