Chain promises and break out by winjs error

In WinJS, what is the correct way to chain promises if you need to “break out” of the chain? For example, if you had the functions .then () and the final .done (). What is the correct way to return an error from the fact that there are 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);
 });

, filePicker, err ( " " ); . then (text), ( "no text" ), undefined. doSomething(). Done() "No File" , , , - "err (" ")" . ? - - ?

*** . , , , :

      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);
        });
+1
3

WinJS.Promise.wrapError, .

, "".

"" " " - , , .

+4

, err ( " " ) "return err ("... ")".

0

winjs.promise.wraperror, ,

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


try catch

try{
somepromise()
.then(  function(){ return someAsyncop()})
.then(  function(){
         somelogfunction("someAsyncop done ok");
         return WinJS.Prmoise.wrap();}
     )
.done( function(){ //dosomtehing});
}
catch(e){
        somelogfunction(e.message);
}
0

All Articles