Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if-else flow in promise (bluebird)

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.

like image 789
vinayr Avatar asked Oct 28 '14 02:10

vinayr


3 Answers

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();
});
like image 78
Bergi Avatar answered Oct 31 '22 13:10

Bergi


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.

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
Actually, in the your case it would simply be
if (someCondition) return somePromise;

inside of the first .then() function.

like image 35
Goblinlord Avatar answered Oct 31 '22 13:10

Goblinlord


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

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 functionA only called after the file maybe was written you can separate:

maybeWrite(condition, file, jsonData)
.then(function(){
    // here file may have been written, you can call 'functionA'
    return functionA();
})
like image 2
aarosil Avatar answered Oct 31 '22 14:10

aarosil