I am writing an Angular2 based mobile app, using Typescript with the Nativescript runtime, but am facing some issues with Promises.I have a HomeComponent, from which I would like to be able to call various functions (wrapped in promises), one of these is the scan promise method. See below:
BLE Utility class:
export class ble {
scan() {
return new Promise((resolve, reject) => {
try {
// my code emitted
}
catch (e) {
reject(e);
}
});
}
}
Angular2 Home Component:
import {ble} from "../../Utilities/newBLEDevice";
export class HomePage {
_ble: ble = new ble;
bluetoothAdd() {
this._ble.scan.then( // <- ERROR LINE
}
}
However, when I do this, I get an error on the this._ble.scan.then line :
[ts] Property 'then' does not exist on type '() => Promise<{}>'
What am I doing wrong?
That error message tells you that you're attempting to access a property on a function. You shouldn't be trying to run then on the function itself. You need to call the function and use then on the resulting Promise. Change this:
this._ble.scan.then(...
to this:
this._ble.scan().then(
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With