Skip to content

Commit

Permalink
emberjs#2264 change in promise chain example and add explanation for it
Browse files Browse the repository at this point in the history
  • Loading branch information
karan-pathak committed Apr 15, 2018
1 parent f58cede commit 8750b06
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions source/getting-started/js-primer.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ myPromiseObject.then(function(value) {
});
```

Let's look at some code to see how they are used in ember:
Let's look at some code to see how they are used in Ember:

```javascript
store.findRecord('person', 1).then(function(person) {
Expand All @@ -195,15 +195,17 @@ Now we can come to part where these promises are chained:
```javascript
store.findRecord('person', 1).then(function(person) {

store.findRecord('post', 1) //query for another record.
return person.get('post'); //get all the posts linked with person.

}).then(function(post){
}).then(function(posts){

store.findRecord('comment',1)
myFirstPost = posts.get('firstObject'); //get the first post from collection.
return myFirstPost.get('comment'); //get all the comments linked with myFirstPost.

}).then(function(comment){
}).then(function(comments){

store.findRecord('book', 1)
// do something with comments
return store.findRecord('book', 1); //query for another record

}).catch(function(err){

Expand All @@ -212,6 +214,8 @@ store.findRecord('person', 1).then(function(person) {
})
```

In the above code snippet, we assume that a person has many posts, and a post has many comments. So, `person.get('post')` will return a `promise` object and we chain the response with `then()` so that when it's resolved, we get the first object from the resolved collection and get comments from it with `myFirstPost.get('comment')` which will again return a `promise` object, thus continuing the chain.

### Resources

For further reference you can consult Developer Network articles:
Expand Down

0 comments on commit 8750b06

Please sign in to comment.