A promise inside a promise

I am trying to write this code using Promise. but I don’t know how to write a promise inside Promise and loop. I tried to think so, but the insertBook function becomes asynchronous. How can I get bookId synchronously?

update: function(items, quotationId) { return new Promise(function(resolve, reject) { knex.transaction(function (t) { Promise.bind(result).then(function() { return process1 }).then(function() { return process2 }).then(function() { var promises = items.map(function (item) { var people = _.pick(item, 'familyName', 'firstNumber', 'tel'); if (item.type === 'book') { var book = _.pick(item, 'name', 'bookNumber', 'author'); var bookId = insertBook(t, book); var values = _.merge({}, people, {quotation: quotationId}, {book: bookId}); } else { var values = _.merge({}, people, {quotation: quotationId}); } return AModel.validateFor(values); }); return Promise.all(promises); }).then(function(items) { var insertValues = items.map(function (item) { return People.columnize(item); }); return knex('people').transacting(t).insert(insertValues); }).then(function() { return process5 }).then(function() { ........... }).then(function() { t.commit(this); }).catch(t.rollback); }).then(function (res) { resolve(res); }).catch(function(err) { reject(err); }); }); } function insertBook(t, book){ return Promise.bind(this).then(function () { return Book.columnizeFor(book); }).then(function (value) { return knex('book').transacting(t).insert(value, "id"); }); } 
+7
javascript promise
source share
2 answers

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"); }); } 
+1
source share

Assuming insertBook returns a promise you could make

 var people = _.pick(item, 'familyName', 'firstNumber', 'tel'); if (item.type === 'book') { var book = _.pick(item, 'name', 'bookNumber', 'author'); return insertBook(t, book) .then(bookId => _.merge({}, people, {quotation: quotationId}, {book: bookId})) .then(AModel.validateFor) } else { return Promise.resolve(_.merge({}, people, {quotation: quotationId})) .then(AModel.validateFor) } 
0
source share

All Articles