Michael Hoeller
Michael Hoeller

Reputation: 24258

How to return a string from a lambda function?

I call the function myFunction() and like to get the source._id returned unfortunately the following code does not work. The source._id is fill and ok but how can I return it complete back? As something like:

var newId = myFunction();

The query and the save are 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);
                            }
                        });
                    }
                }
            })
        };

Upvotes: 0

Views: 2590

Answers (2)

JLRishe
JLRishe

Reputation: 101720

Since you have promises at your disposal, you should use them like promises:

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

Upvotes: 1

carchase
carchase

Reputation: 493

query.exec() and source.save() are asynchronous functions, so when you return source.id it is actually being returned to the async functions and not to your function. To my knowledge there is no way to return a value from an async function and have it fall through to your function. Here are two things that you can try.

Try to return the async functions, this MAY give you the functionality you want.

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

Barring that, you can give your function a callback that lets you get the value after the function has executed.

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

Then to call it you would do

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

Upvotes: 0

Related Questions