我正在从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');
}
当我尝试打印响应时,它使我不确定。
答案 0 :(得分:4)
await
仅适用于promise,并且看来MationPlugin.getGroupAllInfo
使用回调来实现其异步性,而不是promise。您需要自己将其包装在一个诺言中。
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);
});
})