【问题标题】:How to return a string from a lambda function?如何从 lambda 函数返回字符串?
【发布时间】:2016-11-17 08:31:42
【问题描述】:

我调用函数 myFunction() 并希望获取返回的 source._id 不幸的是,以下代码不起作用。 source._id 已填充并且可以,但我怎样才能将其完整返回?比如:

var newId = myFunction();

查询和保存都是猫鼬承诺。

        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);
                            }
                        });
                    }
                }
            })
        };

【问题讨论】:

  • 这个函数是同步的还是异步的?

标签: javascript lambda typescript mongoose promise


【解决方案1】:

既然你可以使用 Promise,你应该像 Promise 一样使用它们:

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));

【讨论】:

    【解决方案2】:

    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 });
    

    【讨论】:

      猜你喜欢
      • 2022-10-14
      • 1970-01-01
      • 2010-12-23
      • 1970-01-01
      • 2017-05-12
      • 2011-03-15
      • 1970-01-01
      • 2010-12-14
      相关资源
      最近更新 更多