Created
February 10, 2018 18:12
-
-
Save Frelseren/e4885ce210b1fcdcf74bf48176b8bd7a to your computer and use it in GitHub Desktop.
Explample of async web service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example of asynchronous web service | |
// Function that returns a promise with a list of contacts | |
// and throws if there is any error in Apex. As alternative, | |
// you can return empty array right there. | |
async getContacts(): Promise<Contact[]> { | |
try { | |
const contacts = await JSON.parse(sforce.apex.execute('AngularPOC', 'getContacts', { })); | |
return contacts; | |
} catch (e) { | |
throw new Error(e); | |
} | |
} | |
this.getContacts() | |
.then(contacts => this.contacts = contacts) | |
.catch(error => this.contacts = []); | |
// By the way, Angular has an async pipe so that you can | |
// display promises directly in the template. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment