1

I have some problems with the multiple promises in my code. There is no way to return to items who are not in the database. I changed the code multiple times but no luck. The only data it returns is "datas": [ null, null ] This is my code

var start = function(offset, entry) {
  return new Promise(function(resolve, reject) {
    rp('************' + entry).then(function(repos) {
      resolve(repos);
    }).catch(function(err) {
      reject(err);
    });
  });
};

var findnewones = function(iten) {
  return new Promise(function(resolve, reject) {
    return Promise.all(iten.items.map(function(ndtrcitem) {
      return new Promise(function(resolve, reject) {
         Items.findOne({"metadata.trcid": ndtrcitem.metadata.trcid}).exec(function(err, doc) {
          if (!doc) {
            resolve(ndtrcitem);
          }
        });
      })
    })).then(datas => {
      resolve(datas);
    });
  })
}


exports.find = function(req, res, next) {
  var ndite = ["locations", "events"];
  var items = [];
  return Promise.all(ndite.map(function(entry) {
    return start(0, entry).then(function(res) {
      for (i = 0; i <= res.count; i += 10) {
        return start(i, entry).then(function(iten) {
          findnewones(iten).then(function(dat) {
            items.push(dat);
          });
        });
      }
      return items;
    })
  })).then(datas => {
    res.json({datas});
  });
}
Wouter
  • 427
  • 1
  • 4
  • 15
  • This is a very good example of https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it . It makes control flow horribly unclear, for starters. No wonder the code doesn't work as intended. Never wrap promises with `new Promise`. Promises were introduced to avoid callback hell, not to create it. – Estus Flask Aug 05 '18 at 20:40

1 Answers1

0

I think because the for loop there is synchronous and it's not waiting for the start() promise to resolve.

for (i = 0; i <= res.count; i += 10) {
    return start(i, entry).then(function(iten) {
      findnewones(iten).then(function(dat) {
        items.push(dat);
      });
    });
  }

I have replaced it with async/await, don't know if it will work right away, I am just providing you with a hint in this very complicated promise chain. If it or any variation of it works please update this answer.

exports.find = function (req, res, next) {
    var ndite = ["locations", "events"];
    var items = [];
    return Promise.all(ndite.map(function (entry) {
        return start(0, entry)
        .then(async function (res) {////// this
            for (i = 0; i <= res.count; i += 10) {
                await start(i, entry).then(function (iten) { ////this
                    findnewones(iten).then(function (dat) {
                        items.push(dat);
                    });
                });
            }
            return items;
        })
    })).then(datas => {
        res.json({
            datas
        });
    });
}
Aritra Chakraborty
  • 12,123
  • 3
  • 26
  • 35