Why do both Promise's then & catch callbacks get called?

后端 未结 4 458
旧巷少年郎
旧巷少年郎 2021-01-07 21:24

I have the following code and when it\'s executed, it returns both \"rejected\" and \"success\":

// javascript promise
var          


        
4条回答
  •  庸人自扰
    2021-01-07 21:59

    For those who had a successfully resolved promise and a chain ordered like .then > .catch, but still had both your then and catch called, it may be because your then had an error-throwing bug that you can't see unless you explicitly console the error in your catch. That's one of my pet-peeves with Promises absorbing errors even in strict mode.

    const promise = new Promise(resolve => resolve())
    .then(() => {
        console.log('then');
        not.defined = 'This causes the catch to fire even though the original promise resolved successfully.';
    })
    .catch((e) => {
        console.log('catch');
        // console.error(e);
    });
    

提交回复
热议问题