小编vmt*_*ran的帖子

Typescript 中使用异步方法链接(刚刚找到了解决方案)

我正在编写一个模块,旨在在将查询调用到数据库之前准备查询。普通 javascript 中的代码运行得很好,但是当我尝试用 Typescript 编写它时,出现了错误:Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member

我的 JavaScript 代码:

class QueryBuilder {
  constructor(query) {
    this.query = query;
  }

  sort(keyOrList, direction) {
    this.query = this.query.sort(keyOrList);
    return this;
  }

  skip(value) {
    this.query = this.query.skip(value);
    return this;
  }

  limit(value) {
    this.query = this.query.limit(value);
    return this;
  }

  then(cb) {
    cb(this.query.toArray());
  }
}
Run Code Online (Sandbox Code Playgroud)

打字稿中的代码:

class QueryBuilder {
  public query: Cursor;
  constructor(query: Cursor) {
    this.query = query;
  }

  public …
Run Code Online (Sandbox Code Playgroud)

javascript chaining node.js async-await typescript

6
推荐指数
1
解决办法
2093
查看次数

标签 统计

async-await ×1

chaining ×1

javascript ×1

node.js ×1

typescript ×1