3 回答
TA貢獻1843條經驗 獲得超7個贊
Menus.getCantinas().then(function(cantinas) {
Menus.cantinas = cantinas;
if(cantinas.length == 0) {
return $.Deferred().reject(errMessages.noCantinas);
} else {
return $.when(Menus.getMeals(cantinas), Menus.getSides(cantinas));
}}).then(function(meals, sides) {
Menus.sides = sides;
Menus.meals = meals;
return Menus.getAdditives(meals, sides);}).then(function(additives) {
Menus.additives = additives;
return Menus;}).then(null, function(err) {
//This "catch" exists solely to detect the noCantinas condition
//and put the chain back on the success path.
//Any genuine error will be propagated as such.
//Note: you will probably want a bit of safety here as err may not be passed and may not be a string.
return (err == errMessages.noCantinas) ? $.when(Menus) : err;}).done(function(Menus) {
// with no cantinas, or with everything});var errMessages = {
'noCantinas': 'no cantinas'};cantinas.length == 0errMessages
TA貢獻1828條經驗 獲得超13個贊
doneif
Menus.getCantinas().then(function(cantinas) {
Menus.cantinas = cantinas;
if (cantinas.length == 0)
return Menus; // break!
// else
return $.when(Menus.getMeals(cantinas), Menus.getSides(cantinas))
.then(function(meals, sides) {
Menus.sides = sides;
Menus.meals = meals;
return Menus.getAdditives(meals, sides);
}).then(function(additives) {
Menus.additives = additives;
return Menus;
});}).done(function(Menus) {
// with no cantinas, or with everything});TA貢獻1815條經驗 獲得超13個贊
thencatchUncaught (in promise)
var noopPromise = {
then: () => noopPromise,
catch: () => noopPromise}function haltPromiseChain(promise) {
promise.catch(noop)
return noopPromise}// Use it thus:var p = Promise.reject("some error")p = haltPromiseChain(p)p.catch(e => console.log(e))
// this never happens- 3 回答
- 0 關注
- 540 瀏覽
相關問題推薦
添加回答
舉報
