You do not need to synchronize the book, you can process it asynchronously. Also, perhaps you want all book insertions to occur sequentially, so I reorganized the Promise.all part. (do this to give you an idea. Promise.all should work fine if parallel inserts are allowed). Also, I think you should not use Promise.bind . Honestly, I don’t even know what he is doing, one thing is for sure: it does not work with standard promises. So here is an example, I think it should work:
update: function(items) { return new Promise(function(resolve) { knex.transaction(function (t) { resolve(Promise.resolve().then(function() { return process1; }).then(function() { return process2; }).then(function() { var q = Promise.resolve(), results = []; items.forEach(function (item) { q = q.then(function() { var book = _.pick(item, 'name', 'bookNumber', 'author'); return insertBook(t, book); }).then(function(bookId) { var people = _.pick(item, 'familyName', 'firstNumber', 'tel'); var values = _.merge({}, people, {book: bookId}); return AModel.validateFor(values); }).then(function(item) { results.push(item); }); }); return q.then(function() { return results; }); }).then(function(items) { return process4 }).then(function() { t.commit(result); }).catch(function(e) { t.rollback(e); throw e; })); }); }); } function insertBook(t, book){ return Promise.resolve().then(function () { return Book.columnizeFor(book); }).then(function (value) { return knex('book').transacting(t).insert(value, "id"); }); }
Tamas hegedus
source share