Saturday 8 April 2017

Implemented Bluebird nested loops promise in Node JS

I'm using bluebird in NodeJS. I want to do a nested loop. Something like this:

  var inputData = [{
      _id: 1,
      title: 'abc'
  }, {
      _id: 2,
      title: 'pqr'
  }];
  var inputSeries = [{
      _id: 1,
      rules: [{
          _id: 1,
          type: 'abc'
      }]
  }]
  var output = [];

 //Loop1
 inputData.forEach(function(fact) {
    //Loop2
    inputSeries.forEach(function(series) {
       //Loop3
        series.rules.forEach(function(rule) {
          //Here I need to call rule engine
          if (processRules(fact,rule)) {
            output.push({
                  'dataId': fact._id,
                  'seriesId': series._id,
                  'ruleId': rule._id
            })
          }
     })
   })
})
    //Rule Engine Function
    var processRules = function(fact, rule) {
        if (fact.title === rule.type) {
            return true;
        }
        return false;
    }

I see some similar question but not getting exact ideas from them.

Please help me to implement nested each loop with bluebird promise.



via Santosh Shinde

No comments:

Post a Comment