Chain promises and break out on error WinJS
Asked Answered
C

3

1

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?

Consider the following pseudo-code example

 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);
 });

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?

thanks

***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);
        });
Cytochemistry answered 4/12, 2012 at 18:43 Comment(0)
C
4

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.

Countryman answered 4/12, 2012 at 22:31 Comment(1)
even if you don't return a value from an error handler halfway through the chain, the return value of the handler (undefined) gets propagated along the success chain, right?Monniemono
B
0

I'm not completely sure, but try just calling err('No text') instead of "return err('...')".

Blindstory answered 4/12, 2012 at 20:33 Comment(0)
A
-1

if you gonna use winjs.promise.wraperror, you should provide errorhandler like this

somepromise()
.then(
        function(){ return someAsyncop()},
        function(error){
         somelogfunction("somepromise failed");
         return WinJS.Promise.wrapError(error);
        })
.then(
        function(){ somelogfunction("someAsyncop done ok"); return WinJS.Prmoise.wrap();},
        function(error){
         somelogfunction("someAsyncop failed"); return WinJS.Promise.wrap();
        })
.done(function(){ //dosomtehing });


or use try catch construction

try{
somepromise()
.then(  function(){ return someAsyncop()})
.then(  function(){
         somelogfunction("someAsyncop done ok");
         return WinJS.Prmoise.wrap();}
     )
.done( function(){ //dosomtehing});
}
catch(e){
        somelogfunction(e.message);
}
Alyosha answered 7/4, 2016 at 9:30 Comment(1)
invalid syntax, "WinJS.Prmoise" other small erros "dosomtehing"Chantellechanter

© 2022 - 2024 — McMap. All rights reserved.