angular await не ждет завершения функции - PullRequest
1 голос
/ 26 февраля 2020

Я вызываю функцию asyn c из плагина cordova. Однако ожидание на самом деле не работает.

export class MationLiteService implements IgatewayService {
  async getGroupAllInfo(gatewayId: string, account: string, decryptedpasswd: string) {

// some codes
        return await cordova.plugins.MationPlugin.getGroupAllInfo(data, async (response) => {

        const json: JSON = JSON.parse(response);
        console.log('responseaaaaa:' + response);
        return json;
      }, (error) => {
        console.log('error: ' + error);
      });
  }
}

вот класс менеджера шлюза

export class GatewayManagerService {
 public  getGroupAllInfo(gatewayType: string, gatewayId: string, account: string, decryptedpasswd: string) {
    return this.gatewayFactoryService.getGateway(gatewayType).getGroupAllInfo(gatewayId, account, decryptedpasswd);
  }
}

вот как я его называю

  async getAllInfo2(){
     this.facadaService.GetGatewayManagerService().getGroupAllInfo('mation-lite', this.zifan, 'admin', '5555').then((response) => {
        console.log(response);
     });
    //await this.facadaService.GetGatewayManagerService().test('mation-lite');

  }

это дало мне не определено, когда я пытаюсь напечатать ответ.

1 Ответ

4 голосов
/ 26 февраля 2020

await работает только с обещаниями, и похоже, что MationPlugin.getGroupAllInfo использует обратный вызов для своей асинхронности, а не обещания. Вам нужно будет завернуть это в обещание самостоятельно.

getGroupAllInfo(gatewayId: string, account: string, decryptedpasswd: string) {
  return new Promise((resolve, reject) => {
    cordova.plugins.MationPlugin.getGroupAllInfo(data, (response) => {
      const json: JSON = JSON.parse(response);
      resolve(json);
    }, (error) => {
      reject(error);
    });
})
...