0

Here i am using validate.js as a validator. I want return the result through promise, but i can't able get the result from the promise. Please can anybdy help me on this. here is my code.

const validate = require("validate.js"),
    Promise = require('bluebird');

var constraints = {
    "boarding": {
        "accesskey": {
            "presence": true
        },
        "secretKey": {
            "presence": true

        },
        "region": {
            "presence": true

        }
    }
};

let validator = {
    keyValidator: function (body) {
        return new Promise((resolve, reject)=> {
            validate.async(body, constraints.boarding).then(function (success) {
              return  resolve(true);
            }, function (error) {
              return  reject(error);
            });
        })
    }
};

console.log("The Result is",validator.keyValidator({accesskey:"1233",secretKey:"sdfsdf",region:"south"}));
Jagadish Upadhyay
  • 1,236
  • 13
  • 22
Jeevan
  • 756
  • 13
  • 39
  • 1
    validator.keyValidator(..) returns a Promise object. You need to use its `then` method to get the value that you need. – Bidhan Jan 05 '17 at 07:18
  • @BidhanA i just started learning the Promises Could you p;ease Explain bit more – Jeevan Jan 05 '17 at 07:21
  • 1
    You can't just `console.log` the result from a Promise because it might take some time to execute. What you need to do is put your console.log() inside its `then` method. Something like this `validator.keyValidator(..).then(function(value) {console.log(value);});` . Read the docs on MDN. – Bidhan Jan 05 '17 at 07:25

0 Answers0