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 bee way longer.
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 bee way longer.
Share Improve this question edited Aug 27, 2019 at 18:39 Pedro asked Jun 6, 2014 at 22:39 PedroPedro 3,8112 gold badges27 silver badges33 bronze badges 5- 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 Commented Jun 6, 2014 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 Commented Jun 6, 2014 at 23:21
-
1
If you
throw
inside yourthen
handler, that will reject the chain. There's also an (open issue)[github./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 Commented Jun 10, 2014 at 8:51 - That sounds great! I will add a ment today. The throw seems to be a good fallback for now too. Thanks for you answer :) – Pedro Commented Jun 11, 2014 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 thethen
as you suggested. github./sequelize/sequelize/pull/934 – Pedro Commented Jun 13, 2014 at 20:20
1 Answer
Reset to default 6Your 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);
});