facebook/react — pull request #1556 by joshma (2014-05-18 04:27:58+00:00)
Title: Update Flux Dispatcher.dispatch and waitFor examples

The previous `Dispatcher.dispatch` and `waitFor` examples didn't properly work 1) when a Store callback does `waitFor` on Stores that haven't been reached yet (the corresponding `promise` would not have existed) and 2) when a Store callback waits on another waiting Store. The updated example uses constructs Promises up front and then asynchronously resolves them. It also lets Store callbacks return Promises, which is the only way I could think of to allow chaining waitFor dependencies.

In my personal implementation I'm using jQuery Deferreds, which give more flexibility in resolving outside the constructor. I'm not 100% sure this is the right way to resolve/reject an ES6 Promise after construction, so someone please let me know if this is a no-no.

(Also, unless the Dispatcher supports nested waitFor() calls, there doesn't seem to be a need to `_clearPromises` after all promises have been resolved, as all callbacks will have been called by then and will have their own references to promises. I found the existing, unnecessary use of `.all()` confusing.)