@@ -446,4 +446,62 @@ UIService.prototype.addEventing = function(eventEmitter) {
446446 });
447447 }.bind (this ));
448448};
449+
450+ ##Method and promise chains
451+
452+ With method- chaining or promise APIs, we may have a long chain of
453+ method calls in a single statement . It is important to format these so
454+ that they are readable and flow as clearly as possible.
455+
456+ ###Place each call on its own line, beginning with the period, and indented
457+
458+ The sequence of operations is clearer if each ' step' in the process
459+ begins at the start of the line . Indenting relative to the previous
460+ line makes the chain distinct.
461+
462+ ` ` ` javascript
463+ // Good
464+ return functionThatReturnsAPromise()
465+ .then(preprocessTheResult)
466+ .catch(handleErrors)
467+ .then(logSomething);
468+
469+ // Bad
470+ return functionThatReturnsAPromise.then(preprocessTheResult)
471+ .catch(handleErrors).then(logSomething);
472+ ` ` `
473+
474+ ###Avoid multiple function expressions in a single function call
475+
476+ The break between two function expressions passed as arguments can be
477+ quite awkward. It is clearer to use named functions, or to change your
478+ use of the API to split them up.
479+
480+ ```javascript
481+ // Bad
482+ functionThatReturnsAPromise ()
483+ .then(function() {
484+ // code here!
485+ }, function () {
486+ // other code here!
487+ });
488+
489+ // Good
490+ var resolveFunction = function () {
491+ // code here!
492+ };
493+ var rejectFunction = function () {
494+ // other code here!
495+ };
496+
497+ functionThatReturnsAPromise ().then (resolveFunction, rejectFunction);
498+
499+ // Also Good
500+ functionThatReturnsAPromise ()
501+ .then (function () {
502+ // code here!
503+ })
504+ .catch (function () {
505+ // other code here!
506+ });
449507```
0 commit comments