Created
July 2, 2018 23:20
-
-
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
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
| 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