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 == 0
errMessages
TA贡献1828条经验 获得超13个赞
done
if
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个赞
then
catch
Uncaught (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 关注
- 480 浏览
相关问题推荐
添加回答
举报