What is faster: try catch vs Promise

后端 未结 3 570
野性不改
野性不改 2020-12-16 01:11

I heard such an opinion that you should avoid usage of try/catch at all because it takes many resources. So could the promise error handling to be faster? Or it does not mat

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-16 01:43

    You should use Promises only for asynchronous functions and nothing else. Do not abuse them as an error monad, that would be a waste of resources and their inherent asynchrony will make every­thing more cumbersome.

    When you have synchronous code, use try/catch for exception handling.

    /* Wrong */
    return new Promise(function(resolve, reject) {
        resolve(x / y);
    }).catch(err => NaN)
    
    /* Right */
    try {
        return x / y;
    } catch(e) {
        return NaN;
    }
    

    Iff you already have promise code, you can avoid that in certain situations: when you want the exception to reject the promise. In those cases you should just let the builtin error handling of your promises do its job, and not complicate everything by an additional but pointless try/catch layer:

    /* Wrong */
    new Promise(function(resolve, reject) {
        try { // when used synchronous in the executor callback
            …
            resolve(somethingSynchronous());
        } catch (e) {
            reject(e);
        }
    });
    
    /* Right */
    new Promise(function(resolve, reject) {
        …
        resolve(somethingExceptionally());
    });
    

    /* Wrong */
    ….then(function(res) {
        try {
            …
            return somethingExceptionally();
        } catch(e) {
            return Promise.reject(e);
        }
    }).…
    
    /* Right */
    ….then(function(res) {
        …
        return somethingExceptionally();
    }).…
    

提交回复
热议问题