Sunday 12 March 2017

Return after a promise is resolved not working

I have a function like this, which should return a schema after query is complete. I am not understanding why this function is always returning undefined value.

var loadSchema = function(className) {
        var query = schemaModel.findOne({className: className})
        var promise = query.exec()

        promise.then(function(schema){
                console.log(schema)
                return schema
        }).then(null, console.log)
}

I also tried to return a whole promise from function but still I cannot get the value. Like this

var loadSchema = function(className) {
        var query = schemaModel.findOne({className: className})
        var promise = query.exec()

        return promise.then(function(schema){
                console.log(schema)
                return schema
        }).then(null, console.log)
}

Can Anyone please tell me the flow of this program. I have watched other similar questions in stackoverflow but I cannot get the whole idea behind this.



via Tolsee

No comments:

Post a Comment