Skip to content

Remove traces of "Ember.run" in run-loop.md #2104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions guides/release/applications/run-loop.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,15 @@ which will make you a better Ember developer.

You should begin a run loop when the callback fires.

The `Ember.run` method can be used to create a run loop.
In this example, `Ember.run` is used to handle an online
The `run` method from `@ember/runloop` can be used to create a run loop.
In this example, `run` is used to handle an online
event (browser gains internet access) and run some Ember code.

```javascript
import { run } from '@ember/runloop';

window.addEventListener('online', () => {
Ember.run(() => { // begin loop
run(() => { // begin loop
// Code that results in jobs being scheduled goes here
}); // end loop, jobs are flushed and executed
});
Expand All @@ -185,15 +187,17 @@ window.addEventListener('online', () => {

## What happens if I forget to start a run loop in an async handler?

As mentioned above, you should wrap any non-Ember async callbacks in `Ember.run`.
As mentioned above, you should wrap any non-Ember async callbacks in `run`.
If you don't, Ember will try to approximate a beginning and end for you.
Consider the following callback:

```javascript
import { schedule } from '@ember/runloop';

window.addEventListener('online', () => {
console.log('Doing things...');

Ember.run.schedule('actions', () => {
schedule('actions', () => {
// Do more things
});
});
Expand All @@ -207,40 +211,42 @@ These automatically created run loops we call _autoruns_.
Here is some pseudocode to describe what happens using the example above:

```javascript
import { begin, end, schedule } from '@ember/runloop';

window.addEventListener('online', () => {
// 1. autoruns do not change the execution of arbitrary code in a callback.
// This code is still run when this callback is executed and will not be
// scheduled on an autorun.
console.log('Doing things...');

Ember.run.schedule('actions', () => {
schedule('actions', () => {
// 2. schedule notices that there is no currently available run loop so it
// creates one. It schedules it to close and flush queues on the next
// turn of the JS event loop.
if (! Ember.run.hasOpenRunLoop()) {
Copy link
Author

@johanrd johanrd Mar 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what to do about this example of Ember.run.hasOpenRunloop() – the method seems undocumented / untyped / without public export / archaic to me.

Ember.run.begin();
begin();
nextTick(() => {
Ember.run.end()
end()
}, 0);
}

// 3. There is now a run loop available so schedule adds its item to the
// given queue
Ember.run.schedule('actions', () => {
schedule('actions', () => {
// Do more things
});

});

// 4. This schedule sees the autorun created by schedule above as an available
// run loop and adds its item to the given queue.
Ember.run.schedule('afterRender', () => {
schedule('afterRender', () => {
// Do yet more things
});
});
```

## Where can I find more information?

Check out the [Ember.run](https://api.emberjs.com/ember/release/classes/@ember%2Frunloop) API documentation,
Check out the [@ember/runloop](https://api.emberjs.com/ember/release/classes/@ember%2Frunloop) API documentation,
as well as the [Backburner library](https://github.com/ebryn/backburner.js/) that powers the run loop.