In my angularJS 4 app, I'm using a cordova accelerometer plugin called device motion for something. The api has a function call getAcceleration(successCallback,errorCallback). So i created a service that looks like this
@Injectable()
export class AccelerometerService {
private acc : any;
constructor(private winRef:WindowRef) {
this.acc = this.winRef.nativeWindow.plugins.navigator.accelerometer;
this.onSuccess = this.onSuccess.bind(this);
this.onError = this.onError.bind(this);
}
getAcceleration(){
return new Promise ((resolve, reject) =>{
resolve(this.acc.getCurrentAcceleration(this.onSuccess,this.onError));
});
}
onSuccess(acceleration){
return acceleration; // value that I want returned to my component
}
onError(){
return 'error';
}
}
In my component, I do this to try to get the return value from onSuccess callback function, however the response is undefined
this.accelerationService.getAcceleration().then((res) =>{
console.log(res); // res is undefined
})
How can I resolve this issue?