admin管理员组文章数量:1026989
console.log('1');
await get()
.then()
.catch(() => {
console.log('2');
return;
});
console.log('3');
Why did console logs 1 2 3? I thought after return statement, the code will be not executed?
console.log('1');
await get()
.then()
.catch(() => {
console.log('2');
return;
});
console.log('3');
Why did console logs 1 2 3? I thought after return statement, the code will be not executed?
Share Improve this question asked Mar 12, 2020 at 3:38 iamhuynqiamhuynq 5,5492 gold badges16 silver badges40 bronze badges2 Answers
Reset to default 6return
will only terminate the current function. Here, the function that gets terminated by the return
is the .catch
callback.
Since .catch
returns a resolved Promise, the await
ed Promise chain will resolve when the catch
resolves.
If you want to stop the outer function when the catch
runs, have your catch
return something that you check outside, eg:
(async() => {
console.log('1');
const result = await Promise.reject()
.catch(() => {
console.log('2');
return 2;
});
if (result === 2) {
return;
}
console.log('3');
})();
Or have the .catch
throw an error, so that the outer Promise chain rejects. Since it's being await
ed, the whole outer Promise will then reject:
const fn = async () => {
console.log('1');
const result = await Promise.reject()
.catch(() => {
console.log('2');
throw new Error();
});
console.log('3');
};
fn()
.then(() => console.log('resolved'))
.catch(() => console.log('rejected'))
If your .catch
doesn't do anything substantial but you want this sort of behavior, it's usually a good idea to omit it entirely. That way, when there's an error, the await
ed Promise will reject, the function will terminate, and the error can be caught by the appropriate consumer.
const fn = async () => {
console.log('1');
const result = await Promise.reject();
console.log('3');
};
fn()
.then(() => console.log('resolved'))
.catch(() => console.log('rejected'))
It's because the return is in a separate function (the catch handler).
The return only applies to the function it is inside of. It will not affect the outer scope.
console.log('1');
await get()
.then()
.catch(() => {
console.log('2');
return;
});
console.log('3');
Why did console logs 1 2 3? I thought after return statement, the code will be not executed?
console.log('1');
await get()
.then()
.catch(() => {
console.log('2');
return;
});
console.log('3');
Why did console logs 1 2 3? I thought after return statement, the code will be not executed?
Share Improve this question asked Mar 12, 2020 at 3:38 iamhuynqiamhuynq 5,5492 gold badges16 silver badges40 bronze badges2 Answers
Reset to default 6return
will only terminate the current function. Here, the function that gets terminated by the return
is the .catch
callback.
Since .catch
returns a resolved Promise, the await
ed Promise chain will resolve when the catch
resolves.
If you want to stop the outer function when the catch
runs, have your catch
return something that you check outside, eg:
(async() => {
console.log('1');
const result = await Promise.reject()
.catch(() => {
console.log('2');
return 2;
});
if (result === 2) {
return;
}
console.log('3');
})();
Or have the .catch
throw an error, so that the outer Promise chain rejects. Since it's being await
ed, the whole outer Promise will then reject:
const fn = async () => {
console.log('1');
const result = await Promise.reject()
.catch(() => {
console.log('2');
throw new Error();
});
console.log('3');
};
fn()
.then(() => console.log('resolved'))
.catch(() => console.log('rejected'))
If your .catch
doesn't do anything substantial but you want this sort of behavior, it's usually a good idea to omit it entirely. That way, when there's an error, the await
ed Promise will reject, the function will terminate, and the error can be caught by the appropriate consumer.
const fn = async () => {
console.log('1');
const result = await Promise.reject();
console.log('3');
};
fn()
.then(() => console.log('resolved'))
.catch(() => console.log('rejected'))
It's because the return is in a separate function (the catch handler).
The return only applies to the function it is inside of. It will not affect the outer scope.
本文标签: javascriptCode still executed after return statement in catchStack Overflow
版权声明:本文标题:javascript - Code still executed after return statement in catch? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745643203a2160870.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论