javascript - 这两种处理顺序 Promise 的风格有什么区别
问题描述
当我们想按顺序依次执行几个 then 函数时,这两种风格有什么区别:
1-使用嵌套的thens
$http.get('api').then(function(){
processBlocks(obj.data).then(function(){
alert('it is done')
});
});
2- 展平嵌套的 then
$http.get('api').then(function(){
return processBlocks(obj.data);
}).then(function(){
alert('it is done')
});
很明显,第二个更具可读性,但也有性能差异吗?
解决方案
$http.get('api').then(function(){
processBlocks(obj.data).then(function(){
alert('it is done')
});
});
在这种情况下,如果您then
像这样连接另一个:
$http.get('api').then(function(){
processBlocks(obj.data).then(function(){
alert('it is done')
});
}).then(function(){
alert('it is done')
});
如果processBlocks()
抛出异常,没关系,下一个promise会被触发,但是:
$http.get('api').then(function(){
return processBlocks(obj.data);
}).then(function(){
alert('it is done')
});
在这种情况下,如果第一个then
失败,则取消序列,如果有catch
块,它将被触发
推荐阅读
- android - 我的 webview 应用程序在另一个应用程序之上打开
- python - 从多个数据帧中提取数据
- asterisk - 没有应用程序“AGI”进行扩展
- angular - Auth Guard 即使在登录后也无法正常工作,总是重定向到登录页面
- unity3d - 我将如何在另一个游戏对象顶部的某个地方获得一个随机位置的 vector3?
- java - java.sql.SQLException:字段“otherMenuItems_id”没有默认值
- c - 如何修复 Linux 系统调用中的分段错误?
- algorithm - 是否有一种快速算法来确定充满变量的矩阵的行列式?
- python - 如何使用 Python 捕获发送到 Web 浏览器的 JSON
- python-3.x - PycURL unsetopt 用于 WRITEFUNCTION