Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure
await settled()
avoids creating an autorun.
Long ago in a galaxy far far away, Ember forced RSVP.Promise to "resolve" on the Ember.run loop. At the time, this was meant to help ease pain with folks receiving the dreaded "auto-run" assertion during their tests, and to help ensure that promise resolution was coelesced to avoid "thrashing" of the DOM. Unfortunately, the result of this configuration is that code like the following behaves differently if using native `Promise` vs `RSVP.Promise`: ```js console.log('first'); Ember.run(() => Promise.resolve().then(() => console.log('second'))); console.log('third'); ``` When `Promise` is the native promise that will log `'first', 'third', 'second'`, but when `Promise` is an `RSVP.Promise` that will log `'first', 'second', 'third'`. The fact that `RSVP.Promise`s can be **forced** to flush synchronously is very scary! Now, lets talk about why we are configuring `RSVP`'s `async` below... --- The following _should_ always be guaranteed: ```js await settled(); isSettled() === true ``` Unfortunately, without the custom `RSVP` `async` configuration we cannot ensure that `isSettled()` will be truthy. This is due to the fact that Ember has configurate `RSVP` to resolve all promises in the run loop. What that means practically is this: 1. all checks within `waitUntil` (used by `settled()` internally) are completed and we are "settled" 2. `waitUntil` resolves the promise that it returned (to signify that the world is "settled") 3. resolving the promise (since it is an `RSVP.Promise` and Ember has configured RSVP.Promise) creates a new Ember.run loop in order to resolve 4. the presence of that new run loop means that we are no longer "settled" 5. `isSettled()` returns false 😭😭😭😭😭😭😭😭😭 This custom `RSVP.configure('async`, ...)` below provides a way to prevent the promises that are returned from `settled` from causing this "loop" and instead "just use normal Promise semantics". 😩😫🙀
- Loading branch information