Promisifies an Apex controller's @RemoteAction methods.
npm install -S promisify-remote-actions
import promisifyRemoteActions from 'promisify-remote-actions';
const ctrl = promisifyRemoteActions(window.MyApexController);
ctrl.sum([1, 2, 3])
.then(sum => {
// sum = 6
return ctrl.divide(sum, 3);
})
.then(avg => {
// avg = 2
return ctrl.divide(avg, 0);
})
.catch(err => {
console.assert(err.status === false);
console.assert(err.message === 'Divide by 0');
});
public class MyApexController {
@RemoteAction
public static Integer sum(List<Integer> values) {
Integer res = 0;
for (Integer value : values) {
res += value;
}
return res;
}
@RemoteAction
public static Decimal divide(Integer dividend, Integer divisor) {
return dividend / divisor;
}
}
yarn test
yarn build