From 8750b0616698010fbef18ab72c0ca029bf49d573 Mon Sep 17 00:00:00 2001 From: karan-pathak Date: Sun, 15 Apr 2018 15:46:51 +0530 Subject: [PATCH] #2264 change in promise chain example and add explanation for it --- source/getting-started/js-primer.md | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/source/getting-started/js-primer.md b/source/getting-started/js-primer.md index 1e37c163d..620d9c6ae 100644 --- a/source/getting-started/js-primer.md +++ b/source/getting-started/js-primer.md @@ -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) { @@ -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){ @@ -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: