javascript - 以推荐的方式停止 Promise Chain 执行

标签 javascript web promise es6-promise

<分区>

我有一个类似于这个的代码:

promise_function().then(()=>{
  //do something
  return another_promise_fucntion();
}).then(() => {
  //do something
  return another_promise_function1();
}).then((result) => {
  //check if result is valid
  if(!result)
     //break chain (how to stop calling the next .then functions ?)
  else
     return another_promise_function2();
}).then(()=>{
   //do something
   return another_promise_function3();
}).catch((err)=>{
   //handle error
});

如果返回的结果无效,我想停止调用下一个 .then() 函数。

我使用了“throw new Error()”,它工作得很好,但我不确定这是否是推荐的方式。

最佳答案

Mozilla 重新启动的正确方法 您可以在此处查看更多详细信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then#Chaining

关于javascript - 以推荐的方式停止 Promise Chain 执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43571086/

相关文章:

javascript - 关于 deviceorientation 事件

java - 元素返回空字符串

javascript - 如何从谷歌电子表格中提取数据到网站?

javascript - 异步/等待不按预期工作

javascript - 如何在 grecaptcha.execute() 完成执行后调用函数 - 由事件触发?

javascript - 有什么方法可以将状态变量导出/传递到 ReactJS 中的外部 CSS 文件

javascript - 具有编译功能和 ng-show 的 Angular 对话框指令不起作用

javascript - bootstrap 组件的导航侧边栏 + 动画部分指示器

javascript - Phonegap 相机插件在 iOS 中不起作用

node.js - 在 Node 中保存来自 REST POST 调用的数据