简体   繁体   中英

How to refactor “if x return x” statements

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM