# What is an "async relationship"? / \`{async: true}\` vs. \`{async: false}\`

**URL:** <https://discuss.emberjs.com/t/what-is-an-async-relationship-async-true-vs-async-false/4107>\
**Category:** Ember Data\
**Created:** [February 7, 2014, 4:28pm UTC](https://discuss.emberjs.com/t/what-is-an-async-relationship-async-true-vs-async-false/4107 "2014-02-07T16:28:09Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![abdull](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@abdull](https://discuss.emberjs.com/u/abdull)\
**Post date:** [February 7, 2014, 4:28pm UTC](https://discuss.emberjs.com/t/what-is-an-async-relationship-async-true-vs-async-false/4107/1 "2014-02-07T16:28:09Z")

</div>

The Ember Data API mentions the concept of “async relationships”.  
E.g., [`DS.belongsTo(..)`](http://emberjs.com/api/data/classes/DS.html#method_belongsTo) and [`DS.hasMany(..)`](http://emberjs.com/api/data/classes/DS.html#method_hasMany) both allow to provide a hash with key `async`:

```
App.Post = DS.Model.extend({
  comments: DS.hasMany('comment', {async: true})
});

```

What is the difference of `async` being either `true` or `false` and what consequences does it have on application design?

---

<div class="post-metadata">

**Author:** ![abdull](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@abdull](https://discuss.emberjs.com/u/abdull)\
**Post date:** [February 9, 2014, 4:53pm UTC](https://discuss.emberjs.com/t/what-is-an-async-relationship-async-true-vs-async-false/4107/4 "2014-02-09T16:53:24Z")

</div>

@Rengers, thank you! Let’s assume in the following that with `{async: false}`, any related entity _will be in fact_ fetched eagerly (that’s how I understood your words).

Consider the following model/class:

```
App.User = DS.Model.extend({
  friends: DS.hasMany('user', {async: false})
});

```

`friends` is a _reflexive association_.  
So does that mean that in the moment I `find` the first `user` with at least one friend, then this kicks off a cascade of recursive requests? E.g., in a big social network application, finding one user results in a million additional requests?

---

<div class="post-metadata">

**Author:** ![flashesofpanic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/flashesofpanic/32/14191_2.png) [@flashesofpanic](https://discuss.emberjs.com/u/flashesofpanic)\
**Post date:** [February 13, 2014, 2:38pm UTC](https://discuss.emberjs.com/t/what-is-an-async-relationship-async-true-vs-async-false/4107/6 "2014-02-13T14:38:02Z")

</div>

The way I learned it was that `async: false` means that Ember Data expects to _already have_ the related data as part of the original request, and `async: true` means it will need to make an additional request to get it (but it won’t necessarily make that request automatically).

In a simple relationship, where a User `hasMany` Stuff,

```
stuff: DS.hasMany('stuff', {async: false})

```

then Ember Data is expecting a request for User to get something like this in response:

```
{
stuff: [{ id: 1, user_id: 1 }, { id: 2, user_id: 1 }, { id: 3, user_id: 1 }],
user: { id: 1, stuff_id: [1, 2, 3] }
}

```

but if the relationship was

```
stuff: DS.hasMany('stuff', {async: true})

```

then Ember Data is just expecting this:

```
{
user: { id: 1, stuff_id: [1, 2, 3] }
}

```

…and if you load the user and ask for `user.get('stuff')` then it will make a separate request for its related Stuff objects. So it may be more helpful to think of the `async:` value not as telling Ember Data how to fetch things, but as telling Ember Data how to _expect_ things from the server.

I’ve only recently tangled with this, though, so I’d be happy to be corrected if I have it wrong.

---

<div class="post-metadata">

**Author:** ![lookingsideways](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/lookingsideways/32/13652_2.png) [@lookingsideways](https://discuss.emberjs.com/u/lookingsideways)\
**Post date:** [February 13, 2014, 4:12pm UTC](https://discuss.emberjs.com/t/what-is-an-async-relationship-async-true-vs-async-false/4107/7 "2014-02-13T16:12:40Z")

</div>

Another thing to take into consideration is that

`stuff: DS.hasMany('stuff', {async: true})`

will always return a promise when you call `myModel.get('stuff')`, even if the server sends the data for `stuff` alongside `myModel`.

I believe @wycats said that `{async: true} ` will become the default eventually - [https://github.com/emberjs/data/issues/1443#issuecomment-26607013](https://github.com/emberjs/data/issues/1443#issuecomment-26607013)

---

<div class="post-metadata">

**Author:** ![bkparso](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/bkparso/32/14842_2.png) [@bkparso](https://discuss.emberjs.com/u/bkparso)\
**Post date:** [February 13, 2014, 5:38pm UTC](https://discuss.emberjs.com/t/what-is-an-async-relationship-async-true-vs-async-false/4107/8 "2014-02-13T17:38:55Z")

</div>

Wonder where I find the downvote button!

---

<div class="post-metadata">

**Author:** ![abdull](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@abdull](https://discuss.emberjs.com/u/abdull)\
**Post date:** [February 17, 2014, 2:32pm UTC](https://discuss.emberjs.com/t/what-is-an-async-relationship-async-true-vs-async-false/4107/9 "2014-02-17T14:32:01Z")

</div>

Thanks @flashesofpanic, your described behavior is almost\* what I observed on `Ember 1.3.1 ` and `Ember Data 1.0.0-beta.6`:

Given:

```
App.Post = DS.Model.extend({
  comments: DS.hasMany('comment', {async: true})
});

```

then a query `this.store.find("post", 12345)` will result in an `HTTP GET` request to `posts/12345`. _Ember Data_ expects as a response something like:

```
{
  post: {
    id: 12345,
    comments: [457459, 98989898, 3643434]
  }
}

```

(\*… regarding “almost”: I’m using `Ember 1.3.1 ` and `Ember Data 1.0.0-beta.6`. Notice in my above example, the server’s JSON response is expected to contain a key named `comments`. A key named `comment_id` or `comments_id` doesn’t work to my observation)

---

<div class="post-metadata">

**Author:** ![kimroen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/kimroen/32/15491_2.png) [@kimroen](https://discuss.emberjs.com/u/kimroen)\
**Post date:** [February 17, 2014, 3:20pm UTC](https://discuss.emberjs.com/t/what-is-an-async-relationship-async-true-vs-async-false/4107/10 "2014-02-17T15:20:54Z")

</div>

Regarding your “almost” - this is actually because you two are using different adapters.

The [ActiveModelAdapter](http://emberjs.com/api/data/classes/DS.ActiveModelAdapter.html) expects:

```nohighlight
{
  post: {
    id: 12345,
    comment_ids: [457459, 98989898, 3643434]
  }
}

```

[RESTAdapter](http://emberjs.com/api/data/classes/DS.RESTAdapter.html) (the default) expects:

```nohighlight
{
  post: {
    id: 12345,
    comments: [457459, 98989898, 3643434]
  }
}

```

---

<div class="post-metadata">

**Author:** ![kimek](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/kimek/32/13608_2.png) [@kimek](https://discuss.emberjs.com/u/kimek)\
**Post date:** [January 29, 2018, 12:06pm UTC](https://discuss.emberjs.com/t/what-is-an-async-relationship-async-true-vs-async-false/4107/11 "2018-01-29T12:06:34Z")

</div>

How this works now ?

async for me doesn’t work when I debugging data models on route ?

I’m need to load it the same time one of the data related with my visitor.

This works for me, however maybe there is better solution for that ?

// route file

```
model(params) {
        return Ember.RSVP.hash({
            visitor: this.get('store').findRecord('visitor', params.id).then(function(visitor){
                return Ember.RSVP.cast(visitor.get('quote')).then(function(){
                    return visitor;
                });
            })
        });
    },

```

---

<div class="post-metadata">

**Author:** ![Thilina\_Dinith\_Fonse](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/thilina_dinith_fonse/32/16123_2.png) [@Thilina\_Dinith\_Fonse](https://discuss.emberjs.com/u/Thilina_Dinith_Fonse)\
**Post date:** [October 26, 2018, 5:54am UTC](https://discuss.emberjs.com/t/what-is-an-async-relationship-async-true-vs-async-false/4107/12 "2018-10-26T05:54:23Z")

</div>

async= true relationships are not working even if its requested i have a .belongsTo relationship and im requesting the data of it but its not loading. Im using emberdata 2.18.x

---

<div class="post-metadata">

**Author:** ![dknutsen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/dknutsen/32/16471_2.png) [@dknutsen](https://discuss.emberjs.com/u/dknutsen)\
**Post date:** [October 26, 2018, 1:48pm UTC](https://discuss.emberjs.com/t/what-is-an-async-relationship-async-true-vs-async-false/4107/13 "2018-10-26T13:48:31Z")

</div>

@Thilina_Dinith_Fonse You’ll need to provide more details. My guess is the payload for the first model isn’t getting the id of the related model so it doesn’t know what to fetch, but it could be anything. Any errors? What do your API payloads look like for your primary model? And maybe your models and adapters too?

---

<div class="post-metadata">

**Author:** ![ppcano](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ppcano/32/14197_2.png) [@ppcano](https://discuss.emberjs.com/u/ppcano)\
**Post date:** [November 11, 2018, 6:43am UTC](https://discuss.emberjs.com/t/what-is-an-async-relationship-async-true-vs-async-false/4107/14 "2018-11-11T06:43:03Z")

</div>

I am posting to keep the info up to date.

The best explanation I could find is this [EmberMap post](https://embermap.com/notes/83-the-case-against-async-relationships). Note, the author provides some arguments against `async` relationship, but `async` is the default option, a stable API and totally safe to use.

### Async relationships (default)

Accessing the relationship will return a **promise**.

```auto
post.get('comments').then((comments) => {
  // now we can work with the comments
});

```

If the data is not available and the relationship is accessed (for example, from a template or a “consumed” computed property), Ember Data will **automatically fetch** the resources .

### Sync relationships (from docs)

Ember Data resolves sync relationships with the related resources available in its local store, hence it is expected these resources to be loaded before or along-side the primary resource.

##### BelongsTo

```js
export default DS.Model.extend({
  post: DS.belongsTo('post', {
    async: false
  })
});

```

In contrast to async relationship, accessing a sync relationship will always return the record (Model instance) for the existing local resource, or null. But it will error on access when a related resource is known to exist and it has not been loaded.

```js
let post = comment.get('post');

```

##### HasMany

```js
  export default DS.Model.extend({
    comments: DS.hasMany('comment', {
      async: false
    })
  });

```

In contrast to async relationship, accessing a sync relationship will always return a [DS.ManyArray](https://www.emberjs.com/api/ember-data/release/classes/DS.ManyArray) instance containing the existing local resources. But it will error on access when any of the known related resources have not been loaded.

```js
post.get('comments').forEach((comment) => {
});

```

If you are using `links` with sync relationships, you have to use `ref.reload` to fetch the resources.
