Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Promise not compiling in Typescript

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?

like image 230
George Edwards Avatar asked May 12 '26 23:05

George Edwards


1 Answers

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(
like image 135
Mike Cluck Avatar answered May 15 '26 14:05

Mike Cluck