本文介绍了链式承诺并在 WinJS 错误时爆发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WinJS 中,如果您需要突破"链,那么正确的链式承诺是什么?例如,如果您有 .then() 函数和最终的 .done().从 then 后面有其他错误返回错误的正确方法是什么?

In WinJS, what is the correct way to chain promises if you needed to "break out" of the chain? For instance, if you had .then() functions and a final .done(). What is the correct way to return an error from a then that has others after it?

考虑下面的伪代码示例

 function doSomething(){
  return new WinJS.Promise(function(comp,err,prog){

  filePicker.pickSingleFileAsync().then(function(file){
         if(file){ // have a valid file
              return  readTextAsync(File);
         }else{ //else user clicked cancel
              return err('No file');
         }
  }).then(function(text){
              if(text){
                   return comp(text);
              }else{
                  return err('No text');
              }
  }).done(null, function(error){ //final done to catch errors
      return err(error);
  });


  });// end return promise
} //end doSomething();

 doSomething().done(function(text){
        console.log(text);
 }, function(err){
        console.log(err);
 });

现在在这个简化的例子中,如果用户取消 filePicker,他们应该点击第一个 err("No File");然而,这仍然继续调用下一个 .then(text) ,当文本未定义时,它也会返回错误(无文本").整体 doSomething().done() 错误处理程序在此处返回No File",这是我所期望的,但调试显示代码仍然调用承诺的err('No Text')"部分.此时是否有可能真正退出承诺链?我应该看看在这里使用 any 方法吗??

Now in this simplified example, if a user cancels the filePicker, they should hit the first err("No File"); however this still proceeds to call the next .then(text) which would also return an error('no text') as text is undefined. The overall doSomething().done() error handler returns "No File" here which is what I would expect, but debugging shows the code still calls the "err('No Text')" part of the promise. Is it possible to actually exit the promise chain at this point? Should I look at using the any method here some how?

谢谢

***编辑.如果其他人想根据下面接受的答案知道我的解决方案是什么样的,如下所示:

***EDIT . In case others want to know what my solution looked like based on the accepted answer below it is as follows:

      filePicker.pickSingleFileAsync().then(function (file) {
            if (file) {
                return Windows.Storage.FileIO.readTextAsync(file);
            }
            return WinJS.Promise.wrapError("No File");
        }).then(function (text) {
            if (text) {
                return complete(text);
            }
            return error("no text");
        }).done(null, function (err) {
            return error(err);
        });

推荐答案

使用 WinJS.Promise.wrapError 向承诺链返回错误.

Use WinJS.Promise.wrapError to return an error to the promise chain.

在您的示例中,如果中途出现真正的错误",您可以选择只在最后处理错误.

In your example you could chose to just handle the error at the end if there are real "errors" surfaced halfway through.

这也让你有机会纠正"错误,并允许成功案例"继续——如果你从错误处理程序的中途返回一个值,这将是沿着链传播的值成功链.

This also gives you the opportunity to "correct" errors, and allow the "success case" to continue on -- if you return a value from an error handler halfway through the chain, that will be the value that propagates along the success chain.

这篇关于链式承诺并在 WinJS 错误时爆发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 22:32