[Reference url]Javascript Remoting で Async/Await 例

Javascript Remoting で Async/Await 例

https://qiita.com/hrk623/items/30b89575c1478eeced37

<apex:page controller="AsyncAwaitSampleController">
    <script>
    (async () => {
      let accountList = await getAccountList().catch(err => {console.error(err.message); });
      if (accountList) { // エラー時は undefined となるので注意
        console.log(accountList);
      }
    })();

    const REMOTEACTION_OPTIONS = {buffer: true, escape: true, timeout: 120000};
    function getAccountList() {
      return new Promise((resolve, reject) => {
        Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.AsyncAwaitSampleController.getAccountList}',
          (result, event) => {
            if (event.status) resolve(result);
            else reject(event);
        }, this.REMOTEACTION_OPTIONS); // アロー関数を使う場合は this を忘れずに
      });
    }
    </script>
</apex:page>
public class AsyncAwaitSampleController {
    @RemoteAction
    public static List<Account> getAccountList() {
        return [SELECT Id, Name FROM Account];
    }
}

投稿者: kinkun

保有資格 Salesforce Certified Platform App Builder T Salesforce Certified Platform Developer I Salesforce Certified Platform Developer II Salesforce Certified Administrator

コメントを残す

メールアドレスが公開されることはありません。