Search code examples
mysqlnode.jsexpressbluebird

How can i handle nested queries with promises instead of callback?


how can i do the following with bluebird promises. modules i am using are bluebird, promise-mysql

  1. insert in table A and return a_id
  2. insert records in table B using a_id as foreign key
  3. insert records in table C using a_id as foreign key
  4. now response is send to user that all records are added.

I am using native queries.


Solution

  • The general idea is:

    insetA()
      .then(function(a_id){
        return Promise.all([inserB(a_id), inserC(a_id)])
      })
      .then(function(){
         res.send("all good")
      })
      .catch(function(error){
         res.send("some error")
      })
    
    
    function insetA(){
      return new Promise(function (resolve, reject) {
        // inserting A 
        // resolve(a_id)
      })
    }