如何从lambda函数返回一个字符串?

时间:2016-07-14 12:14:22

标签: javascript lambda typescript mongoose promise

我调用函数myFunction()并且喜欢返回source._id,遗憾的是以下代码不起作用。 source._id是填充的,但是我怎样才能完全返回?就像这样:

var newId = myFunction();

查询和保存是mongoose promises。

        var myFunction = () => {
            var query = MyModel.findOne({ user: userId, name: name.name });

            query.exec((err, doc) => {
                if (err) {
                    reject (err);
                } else {
                    if (doc != null) {
                        var msg = "Error Msg here";
                        reject(new ValidationError(msg));
                    } else {
                        var source = new MyModel();
                        source.someUserProp  = userId;
                        source.save((err, doc) => {
                            if (err) {
                                throw (err)
                            }
                            else { 
                                return (source._id);
                            }
                        });
                    }
                }
            })
        };

2 个答案:

答案 0 :(得分:1)

既然你有自己的承诺,就应该像承诺一样使用它们:

var myFunction = () => {
    var query = MyModel.findOne({ user: userId, name: name.name });

    return query.exec().then(doc => {
        if (doc != null) {
            var msg = "Error Msg here";
            throw new ValidationError(msg);
        }

        var source = new MyModel();
        source.someUserProp  = userId;

        return source.save().then(() => source._id);
    });
};

myFunction()
    .then(id => console.log(id))
    .catch(err => console.error(err));

答案 1 :(得分:0)

query.exec()和source.save()是异步函数,因此当您返回source.id时,它实际上是返回到异步函数而不是函数。据我所知,没有办法从异步函数返回一个值,并让它落到你的函数中。以下是您可以尝试的两件事。

尝试返回异步函数,这可以为您提供所需的功能。

        var myFunction = () => {
        var query = MyModel.findOne({ user: userId, name: name.name });

        **return** query.exec((err, doc) => {
            if (err) {
                reject (err);
            } else {
                if (doc != null) {
                    var msg = "Error Msg here";
                    reject(new ValidationError(msg));
                } else {
                    var source = new MyModel();
                    source.someUserProp  = userId;
                    **return** source.save((err, doc) => {
                        if (err) {
                            throw (err)
                        }
                        else { 
                            return (source._id);
                        }
                    });
                }
            }
        })
    };

除此之外,您可以为函数提供一个回调函数,让您在函数执行后获取值。

        var myFunction = (callback) => {
        var query = MyModel.findOne({ user: userId, name: name.name });

        query.exec((err, doc) => {
            if (err) {
                reject (err);
            } else {
                if (doc != null) {
                    var msg = "Error Msg here";
                    reject(new ValidationError(msg));
                } else {
                    var source = new MyModel();
                    source.someUserProp  = userId;
                    source.save((err, doc) => {
                        if (err) {
                            throw (err)
                        }
                        else { 
                            callback(source._id);
                        }
                    });
                }
            }
        })
    };

然后调用它你会做

myFunction((id) => { //Can access id in here });