0

I have two Sequelize queries that I need to run in a specific order, the first creates a model and the second creates multiple models from an array. It works in that it inserts obj1 first and then the array of transactions, however the final 'then()' doesn't seem to get called... What am I doing wrong?

var fn = function addTransaction(transaction) {
     return new Promise(function() {
          return Transaction.create(transaction, { fields: [ 'Id', 'Name' ]});
     }
};

var transactions = [...]; //array of transaction objects

Model1.create(obj1, { fields: [ 'Id', 'Name' ,'Description' ]}).then(function() {

    var actions = transactions.map(fn);
    var results = Promise.all(actions).then(function(data) {
        return Promise.all(data.map(fn));
    });
    results.then(function() {
        console.log("Done?"); //not getting called
    });

}).catch(function(err) {
    console.log(err);
});
meanrims
  • 65
  • 8

2 Answers2

2

addTransaction() is never going to resolve the promise it creates. You create an outer promise and return it, but never resolve it. You would at least have to declare a resolve argument to that callback and then call resolve() somewhere.

But, if Transaction.create() already returns a promise, then you can change addTransaction() to this:

var fn = function addTransaction(transaction) {
     return Transaction.create(transaction, { fields: [ 'Id', 'Name' ]});
};

It's also unclear why you're using the same fn to process both the transactions array and the data array that results from each of those transactions. That seems a little odd from a logic point of view. Are you sure that's the correct way to write your code? For us to help you with the design of that part of the code, you'd have to explain more about what you're trying to accomplish by calling fn on both the first array of transactions and then again on the results from that.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 1
    Oh, thats a good point. The function passed into a `Promise` constructor must at least take in the `resolve` parameter. – Sanketh Katta Feb 21 '16 at 00:35
  • I tried removing the outer Promise but now I get 'not null violations' when it tries to create the Model. The var fn = code came from another stack overflow answer: http://stackoverflow.com/questions/31413749/node-js-promise-all-and-foreach by Benjamin Gruenbaum – meanrims Feb 21 '16 at 00:38
  • @meanrims - This sounds like you're just not doing things properly with your your database. If you can add to your answer the correct sequence of operations on your database, we can easily help you execute that properly using promises, but I don't know your database so can't help you fix your raw DB code. That Benjamin answer contained an `fn` that created a promise and resolved it, something your code was NOT doing. But, if `Transaction.create()` already returns its own promise, you can just return (as Benjamin's answer also says). – jfriend00 Feb 21 '16 at 00:42
  • @meanrims - And, as my answer says, it looks wrong that you're already trying to run the same `fn` function on both Transactions and then the result of creating those transactions. There are very few occasions in coding where that's a correct thing to do. You probably need to back up about 5 steps and explain exactly what you're trying to do with your database and describe the exact sequence of operations you want performed and in what order. We can then help you accomplish that with promises. – jfriend00 Feb 21 '16 at 00:46
  • @jfriend00 The db code is just a Sequelize model definition, no other code is needed except the create() call to save to the db – meanrims Feb 21 '16 at 00:48
  • @jfriend00 I removed the duplicate fn call and that, combined with returning that promise seemed to have fixed it. cheers. – meanrims Feb 21 '16 at 00:51
0

I'm not sure if this will solve your problem exactly, but I think you are missing a return:

...
return results.then(function() {
    console.log("Done?"); //not getting called
});
...

Without it the Model1.create promise would resolve before the results finish.

Sanketh Katta
  • 5,961
  • 2
  • 29
  • 30