javascript - 为什么`then`甚至存在,什么时候应该使用它?
问题描述
如果 Promises 的目标之一是防止回调地狱,那么为什么会then
存在以及何时应该使用它?
例子:
//If this
someFunction.then((retunValue) => {
});
//Can be written like so:
const returnValue = await someFunction();
为什么有人会选择第一种方式来编写它?
今天应该在什么时候then
使用?
解决方案
then
早于async
/await
几年。
即使then
您避免回调地狱,因为您可以链接它(并使用间隙变量作为链接的一部分)而不是嵌套回调。
推荐阅读
- swiftui - SwiftUI 中有 macCatalyst 的 targetEnvironment 吗?
- css - 我正在尝试学习实现响应式/移动友好的导航
- csv - 如何更改 .csv 文件中日期的格式
- visual-studio-code - 带有不需要的行为的更漂亮或 VSCode 格式。添加 {' '}
- python - 为什么我不能在 Scala 中正确地用 "\\003" 或 "\\x03" 分割字符串?
- monetdb - MonetDB - 如何创建一个新用户/角色以完全访问整个数据库/模式?
- c++ - 使所有项目(子目录)都可以访问文件夹中的所有文件
- mysql - 通过 ForeignKey 外部引用过滤的汇总子查询注释
- node.js - 通过关系模型中的字段对订单数据进行序列化
- python - 在并发请求的情况下,将数据存储在 Django 应用程序的“线程本地存储”中是否安全?