Lightning Component中使用Promise处理Apex服务端请求

In Lightning Components, each communication with Apex Server-Side is handled as a Javascript async request, and the callback you set is called after the server-side action is completed. A server-side action can return any JSON object, such as sObject or a map of name-value pairs.

The followoing sample code shows how a server-call works in a Lightning Component.

SampleAura.cmp

SampleAuraController.js

SampleAuraController.cls

If there have multiple requests, you need to define multiple actions and callback functions for them, then the source code may looks like below :

SampleAuraController.js

Using Promise in Javascript is a way to avoid this “Callback Hell“, and fortunately, Promise can be used in Lightning Compoenent, as below:

SampleAuraController.js

SampleAuraHelper.js

Then the source code looks more clean and more elegant, isn’t it?

More about Javascript Promise you can reference here:
https://developers.google.com/web/fundamentals/primers/promises

Enjoy it!

Related Post

Pipeline for Salesforce – Be less busy, less... Overview Pipeline is a metadata deploy app for Salesforce, supports: ・Making Changeset with Excel file in seconds. ・Exporting Pipeline target metadata list to Excel file. ・No dependencies on Salesforce Org, deploying metadata between any different organizations. ・Deploying metadata base o...
Salesforce批量编辑 (Chrome扩展) – 在任何标准视图上进行批量插入, ... "Salesforce批量编辑"是今年发布的第二个Chrome扩展功能(第一个在这里),具备以下机能: ・轻松将标准列表视图转变成功能强大的批量编辑器 ・批量插入,批量拷贝,批量更新,批量删除,所有功能都具备,同时支持Classic跟Lightning! ・支持将数据导出到CSV文件中 ・支持从Excel直接拷贝粘贴复数行・复数列的数据 "Salesforce批量编辑"支持几乎所有的Salesforce列表视图、no页面跳转,no附加设定,no kidding。 注意 : 在Classic上使用时,请确保不要勾选“Session Settings”中的“Require H...
Salesforce Smart Importer – 1-Drag to import... This is my third Chrome Extension named Salesforce Smart Importer (Other two popular extensions are Salesforce Mass Editor and Salesforce DevTools), it is designed for Salesforce not only admin but also normal user, supports : ・1-Drag to import any Excel / CSV data on anywhere in Salesforce, both ...
用Sublime Text 3架设Salesforce的快速开发环境... Why Sublime Text 3 ? 工作中接触SFDC已近两年,一直都是使用Eclipse+Force.com IDE的开发环境(Eclipse IDE安装参考此处)。虽说用着相当顺手,总是有些美中不足的地方。比如: 1.对APEX,VF语法支持的不好 ※要知道没有自动联想功能对程序猿而言,就如用Emeditor写Perl一样。 2.程序太重 ※偶尔会没有响应,重新打开还会莫名地把本地的代码自动提交到Salesforce上。。 3.没有很好用的黑色主题(想装逼都装不了。。) 最近试用了下Sublime Text 3,给我最大的感受就是,轻! 整个安装包10M不到。...