1

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.

4
  • 1
    But you are still using async in your code, more so without await Commented Mar 28, 2020 at 16:33
  • You can just do Promise.resolve(employeeObject) in your first .then, and it will be available in your second .then. Commented Mar 28, 2020 at 16:35
  • @AnuragSrivastava, yes I know. Please just assume that I don't want to use async/await in my other function. I want to know whether this thing is achievable using traditional promises or not. Commented Mar 28, 2020 at 16:43
  • @UtsavPatel, I have to get to the second then to resolve the VerifyEmp promise so that I can get that 'verified' msg. I can't resolve { emp: emp, verificatonMsg: msg } in the first then because I don't have verificationMsg yet. Commented Mar 28, 2020 at 16:46

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

2 Comments

This makes no sense. First off, you will know whether verifyEmp() returns a promise or not. If you don't, then something else is wrong. If you really want to wrap the return value of verifyEmp() in a promise, then just do this Promise.resolve(verifyEmp()).then(ver => [emp,ver]). There's no reason for the extra async function.
It's not about typing savings, it's about writing code that is declarative and is obvious what/why it's written that way. I say again, if you don't know what something returns, that's a serious problem. If you actually are trying to use a function that sometimes returns a promise and sometimes returns a value, then throw that library away as that's a broken interface or fix the code. A sometimes asynchronous interface should always return a promise.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.