Skip to content

Instantly share code, notes, and snippets.

@Frelseren
Created February 10, 2018 18:12
Show Gist options
  • Save Frelseren/e4885ce210b1fcdcf74bf48176b8bd7a to your computer and use it in GitHub Desktop.
Save Frelseren/e4885ce210b1fcdcf74bf48176b8bd7a to your computer and use it in GitHub Desktop.
Explample of async web service
// 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