SharePoint Pals
 | Sign In
How to use Angular Promise using Typescript await and async (ES6)
by Krishna KV 24 Oct 2016
Author
Team Leader, Aspire Systems
Visits   
Today  :   12     Total  :    1304

The typescript async and await is a ES6 functionality. It used to allow the developers to write the asynchronous code to flows as a synchronous method, without write a handler or a callback function. The similar pattern was used in C# to make use of the promises.

AppService.ts

 class AppService {
     static $inject = ['$http'];
     constructor(private $http: ng.IHttpService) {
     }
 
     public getBooks() {
         var url = "http://localhost/appservice/api/books";
         return this.$http({
             method: 'GET',
             url: url,
             headers: { "Accept": "application/json; odata=verbose" }
         });
     }
 }

App.ts

 class App {
     
     public static  app: ng.IModule
     constructor() {
       
         App.app = angular.module('app', []);
         App.app.controller("homeCtrl", Home);
         App.app.service('appService', AppService);
     }
 }
 new App();
 
 class Home {
     public lstBooks: Book[];
     static $inject = ['appService'];
     constructor(private appService: AppService) {
 
     }
 public async getBooks() {
         console.log('Before');
          await this.appService.getBooks().then(books =>{
              this.lstBooks = <Book[]>books.data;
             console.log(this.lstBooks);
         });
          console.log('After');
        
 
     }
 }
 

Index.html

 <div ng-controller="homeCtrl as vm">
 	<div ng-show="vm.show">{{vm.process}}</div>
 	<table>
 		<tr>
 			<th>ID</th>
 			<th>Title</th>
 			<th>Author</th>
 		</tr>
 
 		<tr ng-repeat="book in vm.lstBooks">
 			<td>{{book.Id}}</td>
 			<td>{{book.Title}}</td>
 			<td>{{book.AuthorName}}</td>
 		</tr>
 	</table>
 	<button ng-click="vm.getBooks()" type="button">Click</button>
 </div>
blog comments powered by Disqus

SharePoint Pals

Pals
SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile

Training

Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community




SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter November 21, 21:08
Implementing Multi-Threading with MaxDegreeOfParallelism in SharePoint using CSOM – An Insight - https://t.co/RNM7eVuyaa

Twitter November 21, 12:48
Configure Windows Firewall for SQL Server Analysis Service (SSAS) - https://t.co/re2H0cnGwn

Twitter November 21, 12:46
Implementing Multi-Threading in SharePoint To Upload Files using CSOM C# – An Insight - https://t.co/18QwUQG2eR

Twitter November 20, 21:48
How to Get the Client Context Using App Access token, by passing Client ID using CSOM in SharePoint Office 365 - https://t.co/wpmlJfTeWx

Twitter November 18, 21:30
How to Enable Auto Approval For Permission Requests in Community Site Template – Office 2013 using Client Side Object Model (CSOM) C#

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation