问题
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);
});
回答1:
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.
回答2:
I'm not completely sure, but try just calling err('No text') instead of "return err('...')".
回答3:
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);
}
来源:https://stackoverflow.com/questions/13709739/chain-promises-and-break-out-on-error-winjs