javascript - Promise 不在 Typescript 中编译

标签 javascript typescript promise nativescript

我正在编写一个基于 Angular2 的移动应用程序,将 Typescript 与 Nativescript 运行时一起使用,但我面临着 Promises 的一些问题。我有一个 HomeComponent,我希望能够从中调用各种函数(包装在 promises 中),其中之一是 scan promise 方法。见下文:

BLE 实用类:

export class ble {
    scan() {
        return new Promise((resolve, reject) => {
            try {
                // my code emitted
            }
            catch (e) {
                reject(e);
            }
        });
    }
}

Angular2 主页组件:

import {ble} from "../../Utilities/newBLEDevice";
export class HomePage {
    _ble: ble = new ble;
    bluetoothAdd() {
        this._ble.scan.then( // <- ERROR LINE
    }
}

但是,当我这样做时,this._ble.scan.then 行出现错误:

[ts] Property 'then' does not exist on type '() => Promise<{}>'

我做错了什么?

最佳答案

该错误消息告诉您您正在尝试访问函数的属性。您不应该尝试在函数本身上运行 then。您需要调用该函数并对生成的 Promise 使用 then。改变这个:

this._ble.scan.then(...

为此:

this._ble.scan().then(

关于javascript - Promise 不在 Typescript 中编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37492888/

相关文章:

javascript - 如果响应 : 1 then window. 父级

typescript - 使用对象解构来分配类成员

javascript - 使用 Angular HttpClient 在 DELETE 请求完成后执行某些操作

javascript - .then(Promise.mapSeries(...)) 的行为是否与 .then(function() { return Promise.mapSeries(...); }) 不同?

javascript - Uncaught (in promise) 是什么意思?

angularjs - Jasmine 测试返回 promise 的 ajax 请求

javascript - Thymeleaf 表单使用 ArrayList 对象提交

javascript - 在同一页面上将 Javascript 变量传递给 php

javascript - 过滤不在另一个数组中的数组

typescript - 检查数组中是否存在路径的最佳方法是什么?