skip to Main Content

I am trying to call 2 different .then() on the same Sequelize promise.
I do not need to chain the then(), I just want to call 2 different effects when the promise is resolved.

The first then() is called just after creating the promise, in the same function which is creating it.
The second one is called on the promise object after it has been returned to the main code.

Here is my main code :

const Status = require('../controllers/status.controller');

// The dataArray comes from somewhere else in the code and is OK
command = Status.save(dataArray);

console.log(command + ' display 1');

command.then(
     queryResult => {

         console.log(queryResult + ' display 2');

         if(typeof queryResult.command !== 'undefined') {
             console.log(queryResult.command.command);
         }
     });

And the called save() function in ‘../controllers/status.controller’ :

exports.save = (dataArray) => {
    
    var b = B.findOne({
        include: [{
            model: Command
        }],
        where: {
            bId: dataArray[0]
        }
    }).then(
        queryResult => { 

            console.log(queryResult + ' display async');

            status.create({
                bId: queryResult.id,
            });
            queryResult.update({
                lastStatus: dataArray[3],
            });
         }
    );

    console.log(b + ' display 0');
    return b;
}

In both case (display 0 and 1), I got the Sequelize promise.

Problem : in the second then() called from the main code, the query result is null, when it’s the right Sequelize instance in the first one.
I was expecting both to be the same result ("display 2" vs "display async").

I added console logs in the code to display infos on the objects are different steps, here is the result :

[object Promise] display 0
[object Promise] display 1
[object SequelizeInstance:b] display async
undefined display 2

TypeError: Cannot read properties of undefined (reading 'command')

I thought it was possible to launch multiple asynchronous then() on the same promise object if we do not to chained them.
What am I missing in this code ?

2

Answers


  1. Regardless of how you add the .then() callbacks, they’re chained. They execute in the order in which you add them.

    For the second one to get the same data as the first one, the first one would need to return that data. For example:

    .then(queryResult => {
      // your code
      // and then...
    
      return queryResult;
    })
    
    Login or Signup to reply.
  2. In your save function, the b variable is set to the returned value of the then. Adding another then chains the two.

    If you want to run both then independently of each other, you need to store the return value of findOne() in b and add the then seperately:

    exports.save = (dataArray) => {
        
        const b = B.findOne({ // <--- store the Promise from `findOne`
            ...
        })
    
        b.then( // <--- independently chain on b
            queryResult => {
              ...
            }
        );
    
        console.log(b + ' display 0');
        return b; // <--- return the non-chained value
    }
    

    Alternatively, if chain the two, the output of the first then will be put into the second, so you could also return queryResult in the first handler:

    exports.save = (dataArray) => {
        
        const b = B.findOne({
            ...
        }).then(
            queryResult => {
              ...
              return queryResult // <---- propagate queryResult
            }
        );
    
        console.log(b + ' display 0');
        return b;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search