How to perform multiple operations at the same time using sequelize

I know about transactions, but the method of handling callbacks makes it look like the database is hit once. After that, he returns to the server and, as a result, serves the next operation and so on.

I want to do several unrelated operations at the same time in order to be really effective (not chains)

How: user.destroy(); post.create({...}); anotherPost.destroy();

All of them are independent and do not need chains. I just want to do it all at once. How can i do this?

+4
source share
1 answer

The easiest way is to use Promise.props, for example:

var promises = {
     userDestroy = user.destroy(),
     postCreate = post.create(),
     postDestroy = anotherPost.destroy()
};

sequelize.Promise.props(promises).then(function(results) {
  /// each promise is resolved here, results:
  results.userDestroy;
  results.postCreate;
  results.postDestroy;
});

: http://bluebirdjs.com/docs/api/promise.props.html .all, promises: http://bluebirdjs.com/docs/api/promise.all.html

+3

All Articles