Hacker News new | past | comments | ask | show | jobs | submit login

> And there's just about never a reason to use the `Promise` constructor in real world code

Why not? If I want to wrap a callback function in a promised sequence, I don't see a way around it. Please don't suggest importing bluebird/promisifyAll because that only teaches me how to avoid thinking about the problem, not how it's actually solved, let alone why it's a problem to begin with.

  function promiseMeFoo() {
    return new Promise((resolve, reject) => {
      asynchronousFoo((err, out) => {
        if (err) return reject(err);
        return resolve(out)
      });
    });
  }
^ aside from just rearranging the code for legibility, how can I do the same thing without constructing a promise explicitly?



Bluebird's `promisifyAll()` is just a way to generalize that pattern, so that you don't need to write it yourself. And if you care about performance it's substantially faster too. If you want to learn about the pattern then fine, but personally I don't want to write that for every single `fs` function I use in a project, it's much more convenient to just `Bluebird.promisifyAll(require('fs'))`.


Also libraries like `promise-ring` that just do this one thing without needing all of bluebird installed.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: