Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walter/faa85d0f70c84a9d05f4085df48c42d0 to your computer and use it in GitHub Desktop.
Save walter/faa85d0f70c84a9d05f4085df48c42d0 to your computer and use it in GitHub Desktop.
Reasoning around why you should be careful with native `async` functions within Ember app code

From @bendemboski Ember Community Slack -testing channel

link

... I'm pretty sure using native async/await in your application code is what's making you sad For two reasons -- one is that the code that runs after any await call will not be wrapped in a run loop because native promises aren't run loop aware, so anything after an await that uses the run loop, like calling .set on an Ember object, will trigger the auto-run assertion in tests

The other problem you're probably having is that your tests aren't waiting for any of the async behavior to happen Ember.run(() => service.create(name)) doesn't wait for any actual async behavior -- it just drains the run loop queues But your await calls will cause things to actually be async in the browser, and since your test isn't doing anything to wait for that behavior, it will end before all of your code has run So you could try

await Ember.run(() => service.create(name))

and I believe that would get the test to wait for the call to complete

But you're still vulnerable to the autorun assertion I mentioned before, so you probably want to turn those async functions into ember-concurrency tasks, which are runloop-aware

@walter
Copy link
Author

walter commented May 22, 2018

Boils down to when you feel like you should use native async functions with Ember app code, you should ask yourself if said code needs to Ember runloop aware or not.

@walter
Copy link
Author

walter commented May 22, 2018

Further detailed reading (note about native async functions at bottom):

https://discuss.emberjs.com/t/readers-questions-why-does-ember-still-use-rsvp/14736

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment