1

I started to use bluebird instead of Q

Currently the code which I use is like following

runProcess = function(path) {

    var result = Promise.defer();

    fs.readFileAsync(path)
    .then(function (Content) {
            var parser = new parseFile(Content);
            var adpt = parser.update();
            result.resolve(adpt);
    }, function(error) {
            result.reject(error);
    });
    return result.promise;
} 

My Question if there a better way to write it ?

  • 1
    Beter as in, shorter / more performant / more readable.. ? Please specify. – Caramiriel Jan 28 '16 at 08:34
  • @Caramiriel - Shorter and maybe more readable... –  Jan 28 '16 at 08:36
  • Also, if you make a new promise which you do not need to do here, you should use the ES6 standard way which is `p = new Promise(function(resolve, reject) {...});` – jfriend00 Jan 28 '16 at 08:40
  • one liner `runProcess = path => fs.readFileAsync(path).then(content => (new parseFile(content)).update());` - perfectly readable - maybe not – Jaromanda X Jan 28 '16 at 08:42

1 Answers1

1

yes, it can be improved,

step1: stop wrapping promises with promises:

runProcess = function(path) {
   return fs.readFileAsync(path)
    .then(function (Content) {
          var parser = new parseFile(Content);
          return parser.update();
    });
} 

Read about this Promise anti-pattern

In really short ES6 form, it might be:

runProcess = path => fs.readFileAsync(path)
                      .then( content => (new parseFile(content )).update())
Community
  • 1
  • 1
mido
  • 24,198
  • 15
  • 92
  • 117