JessePinkman
JessePinkman

Reputation: 651

How to return a variable along with the promise from a javascript function?

This is where I am stuck:

var verifyEmp = async function () {
    return 'Verified';
}


Employee.find(email, password)
.then((emp) => {
    console.log(emp);
    return verifyEmp();
})
.then((msg) => {
    console.log({ verificationMsg: msg });
})
.catch((err) => {
    console.log(err);
})

As you can see, verifyEmp is a promise returning function(For demo purposes, I have kept this function as simple as possible). So, what I want to achieve is to be able to log { Emp: emp, verificationMsg: msg } in my second then. How do I pass emp variable in the second then while returning the promise.

I know this is conveniently achievable via async/await. I am just exploring how it can be done using traditional promises.

Upvotes: 1

Views: 67

Answers (4)

boyddddd
boyddddd

Reputation: 1

Just return json object instead of string.

var verifyEmp = async function () {
    return 'Verified';
}


Employee.find(email, password)
.then((emp) => {
    console.log(emp);
    return {emp:emp, msg:verifyEmp()};
})
.then((res) => {
    console.log({ Emp: res.emp, verificationMsg: res.msg });
})
.catch((err) => {
    console.log(err);
})

Upvotes: 0

Fullstack Guy
Fullstack Guy

Reputation: 16908

You can return the Promise result from the verifyEmp().then() call (as async functions return a Promise) from the first find().then callback.

There you can pass both the result of the verifyEmp() call and also the emp object from the current scope wrapped in another object to the next then in the chain.

The Promise from the verifyEmp().then() gets automatically unwrapped in the next then callback in the chain:

var verifyEmp = async function () {
    return 'Verified';
}

const Employee = {
  find: async function(email, password){
    return {email, password, "id":123};
  }
}

Employee.find("[email protected]", "test")
.then((emp) => {
    //This promise woud get unwrapped in the next .then callback
    return verifyEmp()
           .then((msg) => ({emp,msg}));
})
.then((msg) => {
    console.log(msg);
})
.catch((err) => {
    console.error(err);
})

Upvotes: 1

Mark
Mark

Reputation: 92440

If you just want to use promises, you can nest a then() into the second one that resolves with the result of the first:

const verifyEmp = async function () {
    return 'Verified';
}

const Employee = {
    async find() {
        return "An employee"
    }
}

Employee.find()
.then(emp => verifyEmp().then(msg => [emp, msg]))
.then(([emp, msg]) => {
    /* do something with amp and msg */
    console.log({emp: emp, verificationMsg: msg });
})
.catch((err) => {
    console.log(err);
})

Upvotes: 1

ibrahim tanyalcin
ibrahim tanyalcin

Reputation: 6491

Didn't test it. Use an async iife or something of sort, if you hate anti-patterns. This solution will work regarless of verifyEmp returning a promise or not.

var verifyEmp = async function () {
    return 'Verified';
}


Employee.find(email, password)
.then((emp) => {
    console.log(emp);
    return (async function(){return verifyEmp()})().then((ver)=>[emp,ver]);
})
.then((msg) => {
    console.log({ verificationMsg: msg });
})
.catch((err) => {
    console.log(err);
})

Upvotes: 0

Related Questions