Search code examples
javascriptnode.jsasync-awaites6-promisebabeljs

right way using async , double then() in promise? node8, need really babel to es5?


UPDATED:

the question about console stringify is duplicated; so i've changed my question to another with async, because is the sam ecode

I have an async function, that get a record throug sequelize orm, why

My question is

const genericResolver = async ( table, action , values ) => {

    resultValues = {};
    let resultValues =   await models[table].findById(values.id).then( function (data) {
      console.log(' ************** data');
      console.log(data);
      return data;
    }).then( function (data2) {
       console.log(' ************** data 2');
       console.log(data2);
      }
    );
    console.log('------ resultValues');
    console.log(resultValues );
    process.exit(1);

for data and data2 I get:

    tour {
      dataValues: 
       { id: 'd61802ff-3eec-4a72-97ca-832f51b96bf0',
         name: 'Chipre 2018',
         price: '1400.00',
         createdAt: 2017-09-05T04:01:27.642Z,
         updatedAt: 2017-10-31T11:29:39.484Z },
      _previousDataValues: 
       { id: 'd61802ff-3eec-4a72-97ca-832f51b96bf0',
         name: 'Chipre 2018',
         price: '1400.00',
         createdAt: 2017-09-05T04:01:27.642Z,
         updatedAt: 2017-10-31T11:29:39.484Z },
      _changed: {},
      _modelOptions: 
       { timestamps: true,
         validate: {},
         freezeTableName: true,
         underscored: false,
         underscoredAll: false,
         paranoid: false,
         rejectOnEmpty: false,
         whereCollection: { id: 'd61802ff-3eec-4a72-97ca-832f51b96bf0' },
         schema: null,
         schemaDelimiter: '',
         defaultScope: {},
         scopes: [],
         hooks: {},
         indexes: [],
         name: { plural: 'tour', singular: 'tour' },
         omitNull: false,
         sequelize: 
          Sequelize {
            options: [Object],
            config: [Object],
            dialect: [Object],
            queryInterface: [Object],
            models: [Object],
            modelManager: [Object],
            connectionManager: [Object],
            importCache: {},
            test: [Object] },
         uniqueKeys: {} },
      _options: 
       { isNewRecord: false,
         _schema: null,
         _schemaDelimiter: '',
         raw: true,
         attributes: 
          [ 'id',
            'name',
            'price',
            'seatsmax',
            'createdAt',
            'updatedAt' ] },
      __eagerlyLoadedAssociations: [],
      isNewRecord: false }

but for 'resultValues' I get:

undefined

I'm doing right with async? i'm using node8 so in theory i don't need babel convert everything to ES5 ? just import or export? I suspect that babel is creating ES6 code

Here my package.json

{
  "name": "myproj",
  "version": "1.0.0",
  "description": "GraphQL server",
  "main": "server.js",
  "scripts": {
    "start": "nodemon ./server.js --exec babel-node -e js",
    "test": "eslint . --ext .js --ext .jsx --ignore-path .gitignore --cache"
  },
  "author": "",
  "license": "MIT",
  "devDependencies": {
    "babel-cli": "^6.24.0",
    "babel-eslint": "^8.0.0",
    "babel-preset-es2015": "^6.24.0",
    "babel-preset-stage-0": "^6.22.0",
    "eslint": "^4.7.1",
    "eslint-plugin-react": "^7.3.0",
    "nodemon": "^1.11.0"
  },
  "dependencies": {
    "bcrypt-nodejs": "0.0.3",
    "body-parser": "^1.17.1",
    "cors": "^2.8.3",
    "dotenv": "^4.0.0",
    "express": "^4.15.2",
    "graphql": "^0.9.1",
    "graphql-server-express": "^0.6.0",
    "graphql-tools": "^0.10.1",
    "jsonwebtoken": "^7.2.1",
    "lodash": "^4.17.4",
    "passport": "^0.4.0",
    "passport-jwt": "^3.0.0",
    "pg": "^7.2.0",
    "sequelize": "",
    "validator": "^6.2.0"
  }
}

Solution

  • This is because objects are passed as reference in JS, so when you do console.log(o), you will see the actual value in console (not the value you passed to it when calling console.log).

    When you use JSON.stringify, you are logging string, which is passed by value. So basically when you use JSON, you will see the value by the time of calling console.log.

    EDIT

    As to your new question, problem is that you do not return data2 from the second then callback. When using async/await, you do not need to use then at all:

    const data = await models[table].findById(values.id);
    

    Or if you want, just return data2 and you should see the same result in resultValues:

    let resultValues = await models[table].findById(values.id).then(data => {
        console.log(' ************** data');
        console.log(data);
        return data;
    }).then(data2 => {
        console.log(' ************** data 2');
        console.log(data2);
    
        return data2; // <== return here
    });
    
    console.log('------ resultValues');
    console.log(resultValues );