首页 > 解决方案 > 为什么`then`甚至存在,什么时候应该使用它?

问题描述

如果 Promises 的目标之一是防止回调地狱,那么为什么会then存在以及何时应该使用它?

例子:

//If this
someFunction.then((retunValue) => {

});

//Can be written like so:
const returnValue = await someFunction();

为什么有人会选择第一种方式来编写它?

今天应该在什么时候then使用?

标签: javascriptpromiseasync-await

解决方案


then早于async/await几年。

即使then您避免回调地狱,因为您可以链接它(并使用间隙变量作为链接的一部分)而不是嵌套回调。


推荐阅读