Friday, 7 April 2017

Using Promise with CouchDB nano & forEach

I need help with debugging this code/or learn efficient way to do it- I tried using bluebird.each to capture all executions within my forEach, but didn't get it work. Same with setting up a new promise with pure javascript. I need help how to execute my forEach FIRST and move on.

let arr = []; 
let fields = ['environment', 'habitat', 'earth]
Promise.each(fields, field => {
  nano.db.use(field).view('options', 'options')
    .spread((body, header) => {
      arr.push(body.rows);
  })
}).then(() => console.log(arr))

-Expected outcome: arr to console.log ====> [1,2,3,4,5]

-Actual outcome: arr is an empty array ====> [ ]

I see it's a problem with asynchronicity, but I can't seem to figure out how to actually make this work. any input or resources will be greatly appreciated!

Thank you



via Harris Lee