How it works...

Promises are simply an abstraction for handling callback functions in asynchronous code, that are easier to read. We declare the result of our service call getPosts as a promise to return BlogPost's array . By calling Promise.resolve, we are returning the Promise object in its asynchronously incomplete state:

getPromiseObject(): Promise<Model[]> {
return Promise.resolve(ASYNC_MODEL_SOURCE);
}

When the source of this promise's data is resolved, we will receive a callback through the then method in our component:

this.service.getPromiseObject().then(object => {/* use object */});

The ES6 arrow function (or fat-arrow), wraps the scope of our promise result so that we can act on it in the same scope of our component that we originally invoked the promise in.

With the post variable, we can access the title, content published, and even author name through the included user model reference of the BlogPost. The strength of Angular's TypeScript type-system is really on show when you are able to relate models easily to each other with tight type-checking.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.71.64