草庐IT

javascript - 处理错误后跳过 promise 链

coder 2024-07-17 原文

使用 https://github.com/kriskowal/q图书馆,我想知道是否有可能做这样的事情:

// Module A

function moduleA_exportedFunction() {
  return promiseReturningService().then(function(serviceResults) {
    if (serviceResults.areGood) {
      // We can continue with the rest of the promise chain
    }
    else {
      performVerySpecificErrorHandling();
      // We want to skip the rest of the promise chain
    }
  });
}

// Module B

moduleA_exportedFunction()
  .then(moduleB_function)
  .then(moduleB_anotherFunction)
  .fail(function(reason) {
    // Handle the reason in a general way which is ok for module B functions
  })
  .done()
;

基本上,如果服务结果不好,我想处理模块 A 中的故障,使用特定于模块 A 内部的逻辑,但仍然跳过 promise 链中剩余的模块 B 函数。

跳过模块 B 函数的明显解决方案是从模块 A 中抛出错误/原因。但是,然后我需要在模块 B 中处理它。理想情况下,我想在不需要任何额外代码的情况下执行此操作模块 B。

这很可能是不可能的 :) 或者违背 Q 的一些设计原则。

在这种情况下,您会建议哪种替代方案?

我有两种方法,但都有其缺点:

  1. 从模块 A 中抛出一个特定的错误并向模块 B 添加特定的处理代码:

    .fail(function(reason) {
      if (reason is specificError) {
        performVerySpecificErrorHandling();
      }
      else {
        // Handle the reason in a general way which is ok for module B functions
      }
    })
    
  2. 在模块A中进行自定义的错误处理,然后在处理完错误后,抛出一个伪造的拒绝原因。在模块B中,添加一个条件来忽略假的原因:

    .fail(function(reason) {
      if (reason is fakeReason) {
        // Skip handling
      }
      else {
        // Handle the reason in a general way which is ok for module B functions
      }
    })
    

方案一需要在模块B中添加模块A的具体代码。

解决方案 2 解决了这个问题,但整个伪造拒绝方法似乎很老套。

您能推荐其他解决方案吗?

最佳答案

让我们谈谈控制构造。

在 JavaScript 中,调用函数时代码以两种方式流动。

  • 它可以返回一个值给调用者,表明它成功完成。
  • 它可以向调用者抛出一个错误,表明发生了异常操作。

它看起来像:

function doSomething(){ // every function ever
  if(somethingBad) throw new Error("Error operating");
  return value; // successful completion.
}

try{
  doSomething();
  console.log("Success");
} catch (e){
  console.log("Boo");
}

Promises 模拟这种完全相同的行为。

在 Promises 中,当您在 .then 处理程序中调用函数时,代码恰好以两种方式流动:

  • 它可以返回一个 promise 或一个表明它成功完成的值。
  • 它可以抛出一个错误,表明发生了异常状态。

它看起来像:

var doSomething = Promise.method(function(){
  if(somethingBad) throw new Error("Error operating");
  return someEventualValue(); // a direct value works here too
}); // See note, in Q you'd return Q.reject()

Promise.try(function(){ // in Q that's Q().then
  doSomething();
  console.log("Success");
}).catch(function(e){
  console.log("Boo");
});

Promises 模型控制流本身

Promise 是对排序操作 本身概念的抽象。它描述了控制如何从一个语句传递到另一个语句。您可以将 .then 视为对分号的抽象。

我们来谈谈同步代码

让我们看看同步代码在您的情况下会是什么样子。

function moduleA_exportedFunction() {
  var serviceResults = someSynchronousFunction();
    if (serviceResults.areGood) {
      // We can continue with the rest of our code
    }
    else {
      performVerySpecificErrorHandling();
      // We want to skip the rest of the chain
    }
}

因此,如何继续我们的其余代码就是简单地返回。这在同步代码和带有 promise 的异步代码中是一样的。执行非常具体的错误处理也是可以的。

我们如何跳过同步版本中的其余代码?

doA();
doB();
doC(); // make doD never execute and not throw an exception
doD();

好吧,即使不是立即,也有一种相当简单的方法可以让 doD 永远不会执行,方法是让 doC 进入无限循环:

function doC() {
    if (!results.areGood) {
      while(true){} // an infinite loop is the synchronous analogy of not continuing
                    // a promise chain.
    }
}

因此,有可能永远无法解决 promise - 就像其他答案所暗示的那样 - 返回一个未决的 promise 。然而,这是非常糟糕的流量控制,因为意图没有很好地传达给消费者,并且可能很难调试。想象一下以下 API:

moduleA_exportedFunction - this function makes an API request and returns the service as a ServiceData object if the data is available. Otherwise, it enters the program into an endless loop.

有点令人困惑,是不是 :)?但是,它确实存在于某些地方。在非常古老的 API 中发现以下内容并不少见。

some_bad_c_api() - this function foos a bar, on failure it terminates the process.

那么,到底是什么困扰着我们终止该 API 中的进程?

一切都是为了责任。

  • 被调用的 API 有责任传达 API 请求是否成功。
  • 来电者有责任决定在每种情况下要做什么。

在你的情况下。 ModelA 只是违反了它的责任限制,它不应该有权对程序的流程做出这样的决定。消费它的人应该做出这些决定。

throw

更好的解决方案是抛出错误并让消费者处理。我将使用 Bluebird promises在这里,因为它们不仅快了两个数量级,而且具有更现代的 API - 它们还具有更好的调试工具 - 在这种情况下 - 用于条件捕获和更好的堆栈跟踪的糖:

moduleA_exportedFunction().then(function(result){
   // this will only be reached if no error occured
   return someOtherApiCall();
}).then(function(result2){
   // this will be called if the above function returned a value that is not a 
   // rejected promise, you can keep processing here
}).catch(ApiError,function(e){
   // an error that is instanceof ApiError will reach here, you can handler all API
   // errors from the above `then`s in here. Subclass errors
}).catch(NetworkError,function(e){
   // here, let's handle network errors and not `ApiError`s, since we want to handle
   // those differently
}).then(function(){
   // here we recovered, code that went into an ApiError or NetworkError (assuming
   // those catch handlers did not throw) will reach this point.
   // Other errors will _still_ not run, we recovered successfully
}).then(function(){
   throw new Error(); // unless we explicitly add a `.catch` with no type or with 
                      // an `Error` type, no code in this chain will run anyway.
});

所以在一行中 - 你会做你会在同步代码中做的事情,就像 promises 通常的情况一样。

注意 Promise.method 只是 Bluebird 用于包装函数的便利函数,我只是讨厌同步投入 promise 返回 API,因为它会造成重大破坏。

关于javascript - 处理错误后跳过 promise 链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24355960/

有关javascript - 处理错误后跳过 promise 链的更多相关文章

  1. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  2. ruby - 如何指定 Rack 处理程序 - 2

    Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack

  3. ruby-on-rails - 跳过状态机方法的所有验证 - 2

    当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested

  4. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  5. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  6. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  7. ruby-on-rails - 错误 : Error installing pg: ERROR: Failed to build gem native extension - 2

    我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby​​'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe

  8. ruby - #之间? Cooper 的 *Beginning Ruby* 中的错误或异常 - 2

    在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee

  9. ruby-on-rails - 每次我尝试部署时,我都会得到 - (gcloud.preview.app.deploy) 错误响应 : [4] DEADLINE_EXCEEDED - 2

    我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie

  10. ruby-on-rails - Rails 5 Active Record 记录无效错误 - 2

    我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa

随机推荐