How to refactor "if x return x" statements

Viewed 80

I am looking for a way to refactoring this "if x return x" pattern. It seems to be redundant.

For example:

async exampleFunction(a) {
    const firstResult = await this.firstHandler(a);

    if (firstResult) {
        return firstResult;
    }

    const secondResult = await this.secondHandler(a);

    if (secondResult ) {
        return secondResult;
    }

    const thirdResult = await this.thirdHandler(a);

    if (thirdResult) {
        return thirdResult;
    }
}
1 Answers

function exampleFunction(a) {
    return this.firstHandler(a) || this.secondHandler(a);
}

Related