Search code examples
javascriptpromisebluebird

Conditionally chaining promises - how to avoid code duplication?


I have the following:

if (someCondition) {
     return promiseMakerA().then(function() {
         return promiseMakerB(someLongListOfArguments);
     });
}
else
    return promiseMakerB(someLongListOfArguments);

How can I eliminate that code repetition (promiseMakerB)?


Solution

  • you can do the following, however, it's not necessarily the most readable way of doing so

    return (someCondition ? promiseMakerA(): Promise.resolve()).then(function() { 
        return promiseMakerB(someLongListOfArguments); 
    });