Sequelize associations - please use promise-style instead
Update: 15 Jan 15 - added .finally()
handler. Also indicated how .then()
is being fed with an argument from the previous handler and how to perform the next sequenced query.
The .success
, .error
and .done
handlers are deprecated. The errors are not critical and it is likely that backwards compatibility will be maintained on them. But you should still change it.
As per promise A specs: http://wiki.commonjs.org/wiki/Promises/A
You now should do the following style:
db.Model.find(something)
.then(function(results) {
//do something with results
//you can also take the results to make another query and return the promise.
return db.anotherModel.find(results[0].anotherModelId);
}).then(function(results) {
//do something else
}).catch(function(err) {
console.log(err);
}).finally(function() {
// finally gets called always regardless of
// whether the promises resolved with or without errors.
// however this finally handler does not receive any arguments.
});
In short:
Use .then
instead of .success
Use .catch
instead of .error
Use .finally
instead of .done
*note: .finally
will always get called regardless.