Book Image

Mastering JavaScript Promises

Book Image

Mastering JavaScript Promises

Overview of this book

Table of Contents (16 chapters)
Mastering JavaScript Promises
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Propagation in Q


The then method always returns a promise that will either be handled or rejected.

In our example code, we assign the output to the reapPromise variable, which will hold the value:

var reapPromise  = getInputPromise()
.then(function (input) {
}, function (reason) {
});

The reapPromise variable is the new promise for the return value of either handler. Only one handler will ever be called and it will be responsible for resolving reapPromise as a function, which can only either return a value (a future value) or throw an exception.

Whatever is the case, there will be the following possible outcomes:

  • If you return a value in a handler, reapPromise will get fulfilled

  • If an exception is thrown in a handler, reapPromise will get rejected

  • If a promise is returned in a handler, reapPromise will become that promise

  • As it will become a new promise, it will be useful for managing delays, combining results, or recovering from errors.

If the getInputPromise() promise gets rejected and you forget...