Skip to content

Instantly share code, notes, and snippets.

@SalesforceBobLightning
Created July 2, 2018 23:20
Show Gist options
  • Select an option

  • Save SalesforceBobLightning/be9d9d169f3cfaa83907abbd926aa28c to your computer and use it in GitHub Desktop.

Select an option

Save SalesforceBobLightning/be9d9d169f3cfaa83907abbd926aa28c to your computer and use it in GitHub Desktop.
Salesforce Lightning JavaScript Helper method that returns a Promise when calling an Apex Controller method
promiseAction : function(cmp, methodName, params){
console.log(methodName);
console.log(params);
return new Promise(function(resolve, reject) {
var action = cmp.get(methodName);
action.setParams(params);
action.setCallback(this, function(response) {
var state = response.getState();
console.log(methodName + ' ' + state);
if(cmp.isValid() && state === "SUCCESS"){
var result = response.getReturnValue();
console.log(result);
resolve(result);
} else if (state === "ERROR"){
var errors = response.getError();
if (errors) {
if (errors[0] && errors[0].message) {
console.log(methodName + ': ' + errors[0].message);
}
}
reject(errors);
}
});
$A.getCallback(function() {
$A.enqueueAction(action);
})();
});
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment