0

I have a sequence of async functions that can be resolved or rejected. This functions must be executed in the correct sequence and depend one on other. So, i used 3 async functions and a try catch block. The problem is that when I reject some of the promises in the async functions, the catch block dont get the error sended on the reject callback. How can I get the error sended on reject? (code bellow)

Or should I use promise chaining? I would like to avoid this...

const methods = {

methodOne: async output => {

    return new Promise( (resolve, reject) => {

        if(error)
            reject(error) // want to get this error in try/catch block
        else 
            resolve()

        })

    })

},

methodTwo: async () => {

    return new Promise( (resolve, reject) => {

        if(error)
            reject(error)
        else 
            resolve('output')

        })

    })

},

methodThree: async () => {

    return new Promise( (resolve, reject) => {

        if(error)
            reject(error)
        else 
            resolve()

        })

    })

},

runMethods: async () => {

    return new Promise( async (resolve, reject) => {

        try {

            await methods.methodOne()
            const output = await methods.methodTwo()
            await methods.methodThree(output)
            resolve()


        } catch(error) {

            console.log(error)
            reject(error)

        }


    })


}

}
  • [Never pass an `async function` as the executor to `new Promise`](https://stackoverflow.com/q/43036229/1048572)! – Bergi Jul 05 '19 at 14:24
  • "*`if(error)`*" - the `error` variable is not declared in your code, so you never reject any of your promises? – Bergi Jul 05 '19 at 14:27
  • "*the catch block dont get the error sended on the reject callback.*" - actually it does, if you did reject one of the promises. Can you please provide an actual [mcve]? – Bergi Jul 05 '19 at 14:28

2 Answers2

0

You don't need the try/catch. You should use the promise .catch when awaiting for the methods.

You also don't need to return new Promise (...).

If your promise is rejected, it will go in the .catch of that promise, wich is different from regular catch.

You could just return nested promises.

runMethods: () => {    
    return methods.methodOne()
            .then(() => methods.methodTwo())
            .then(output  => methods.methodThree(output))                     
}
Vencovsky
  • 28,550
  • 17
  • 109
  • 176
  • 1
    [Never pass an `async function` as the executor to `new Promise`](https://stackoverflow.com/q/43036229/1048572), and avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Jul 05 '19 at 14:25
  • @Bergi That is exaclty why I added to my question `But I also don't think you need to return new Promise(...).` I also made an edit to make it more clear and how to not use `return new Promise(...).` – Vencovsky Jul 05 '19 at 14:28
  • Thanks. Please fix it in the first snippet as well. Using `.catch(reject)` there doesn't really work since it just continues execution of the async function. – Bergi Jul 05 '19 at 14:30
  • @Bergi thanks, just edited the question to make it more clear and better for understanding – Vencovsky Jul 05 '19 at 14:37
0

I usually come at it this way.

const methods = {

    methodOne: () => {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                console.log('method1')
                resolve()
            },1000)
           
        }).catch(e=>console.log(e))
    },
    methodTwo: () => {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                console.log('method2')
                resolve()
            }, 1000)
        }).catch(e => console.log(e))

    },
    methodThree: () => {
        return new Promise((resolve, reject) => {
            
            setTimeout(() => {
                console.log('method3')
                reject('whoops something happened')
            }, 1000)
        }).catch(e => console.log(e))
},

    runMethods: () => {
        let run = new Promise(resolve=>resolve(true))
        run
            .then(result1 => methods.methodOne())
            .then(result2 => methods.methodTwo())
            .then(result3 => methods.methodThree())
            .catch(e=>console.log(e))
    }
}   

methods.runMethods()