9

I'm new to the world of promises and I'm not sure I fully understand how to use them in some cases.

Sequelize recently added support for promises, which really makes my code more readable. A typical scenario is to avoid handling errors several times in infinite callbacks.

The snippet below always return 204, while I'd like it to return 404 when the photo cannot be found.

Is there a way to tell Sequelize to "stop" the execution of the promise chain after sending 404? Note that res.send is asynchronous so it does not stop the execution.

// Find the original photo
Photo.find(req.params.id).then(function (photo) {
    if (photo) {
        // Delete the photo in the db
        return photo.destroy();
    } else {
        res.send(404);
        // HOW TO STOP PROMISE CHAIN HERE?
    }
}).then(function () {
    res.send(204);
}).catch(function (error) {
    res.send(500, error);
});

Of course this example is trivial and could easily be written with callbacks. But in most cases the code can become way longer.

Pedro
  • 3,511
  • 2
  • 26
  • 31
  • What you need is a way to mark the promise as resolved at that point. I am not familiar with sequelize, but I assume there must be a way to do that. – Edwin Dalorzo Jun 06 '14 at 22:53
  • Thanks Edwin. The only thing I know is that Sequelize uses Bluebird. However, I don't see anything in their API that allows me do stop a promise chain. – Pedro Jun 06 '14 at 23:21
  • 1
    If you `throw` inside your `then` handler, that will reject the chain. There's also an (open issue)[https://github.com/sequelize/sequelize/issues/272] to automatically reject the find call if not result is found. You could voice your support there if you need the feature – Jan Aagaard Meier Jun 10 '14 at 08:51
  • That sounds great! I will add a comment today. The throw seems to be a good fallback for now too. Thanks for you answer :) – Pedro Jun 11 '14 at 18:08
  • Ok I posted into a related pull request and got my final answer. Looks like the best solution is to `throw` inside the `then` as you suggested. https://github.com/sequelize/sequelize/pull/934 – Pedro Jun 13 '14 at 20:20

1 Answers1

6

Your promise chains don't necessarily have to be linear. You can "branch off" and create a separate promise chain for the success case, chaining on as many .then()'s as you need to, while having a separate (shorter) promise chain for the failure case.

Conceptually, that looks like this:

         Photo.find
          /     \
         /       \
    (success)   (failure)
       /           \
      /             \
photo.destroy    res.send(404)
     |
     |
res.send(204)

And in the actual code, that looks like this:

// Find the original photo
Photo.find(req.params.id).then(function (photo) {
    if (photo) {
        // Delete the photo in the db
        return photo.destroy().then(function () {
            res.send(204);
        });
    } else {
        res.send(404);
    }
}).catch(function (error) {
    res.send(500, error);
});
Lukas S.
  • 5,698
  • 5
  • 35
  • 50
  • Yeah that's pretty much what I ended up doing. I was expecting Sequelize to simply reject my `find()` promise in order to avoid having too much nested code. Last question: do you know if I need to add a `catch()` for the `destroy()` promise too? Or it will handled by the catch at the end? Thanks! – Pedro Jun 08 '14 at 01:06
  • @PedroCheckos, unless you define a separate `catch()` block within your `Photo.destroy()` promise chain, any unhandled exceptions should "bubble up" to the outer `catch()` block. – Lukas S. Jun 08 '14 at 01:42
  • Awesome, that's all I needed to know! Thanks a lot for the tips :) – Pedro Jun 08 '14 at 03:33
  • 1
    @PedroCheckos, I just stumbled upon this and remembered this question: [Promise Cancellation in bluebird](https://github.com/petkaantonov/bluebird/blob/master/API.md#cancellation). (Note that bluebird is what sequelize.js appears to be using under the hood.) I didn't know that "cancellable promises" were a thing. Further research also led to this question, which has some good information: [Status of cancellable promises](http://stackoverflow.com/questions/21781434/status-of-cancellable-promises). Perhaps this will give you some more ideas to try. – Lukas S. Jun 11 '14 at 02:07