I am calling a async function from cordova plugin. However the await does not really work.
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);
});
}
}
here is the gateway manager class
export class GatewayManagerService {
public getGroupAllInfo(gatewayType: string, gatewayId: string, account: string, decryptedpasswd: string) {
return this.gatewayFactoryService.getGateway(gatewayType).getGroupAllInfo(gatewayId, account, decryptedpasswd);
}
}
this is how i call it
async getAllInfo2(){
this.facadaService.GetGatewayManagerService().getGroupAllInfo('mation-lite', this.zifan, 'admin', '5555').then((response) => {
console.log(response);
});
//await this.facadaService.GetGatewayManagerService().test('mation-lite');
}
it gave me undefined when i try to print the response.
await
only works with promises, and it looks like MationPlugin.getGroupAllInfo
uses a callback for its asynchrony, not a promise. You will need to wrap it in a promise yourself.
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);
});
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.