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){
return readTextAsync(File);
}else{
return err('No file');
}
}).then(function(text){
if(text){
return comp(text);
}else{
return err('No text');
}
}).done(null, function(error){
return err(error);
});
});
}
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);
});