【问题标题】:Sequelize 4: Use native ES6 promise instead of Bluebird promiseSequelize 4:使用原生 ES6 Promise 代替 Bluebird Promise
【发布时间】:2018-01-16 07:56:23
【问题描述】:

是否可以配置 sequelize,使其使用原生 es6 承诺而不是他们的 bluebird 副本?

根据他们的docs with sequelize 4,他们正在使用蓝鸟的独立副本。但我想使用原生 es6 承诺。

示例代码:

public find(selector: object): Promise<ClientEntity[]> {
  // Client is of type Model<ClientModel>; see sequelize-typescript
  return ClientModel.findAll(selector)
    .then(res => res.map(e => e.toJSON()))
    .catch(err => console.warn('PgClientModel#find', err));
}

其中显示以下错误:Type 'Bluebird&lt;void | any[]&gt;' is not assignable to type 'Promise&lt;ClientEntity[]&gt;'. Property '[Symbol.toStringTag]' is missing in type 'Bluebird&lt;void | any[]&gt;'

我现在看到的选项:

  1. 使用蓝鸟
  2. 在 bluebird 的 thencatch 块内,我可以通过原生 es6 承诺 resolvereject。但我实际上不喜欢这个主意。

我正在使用以下软件包

"sequelize": "^4.31.2",
"sequelize-typescript": "^0.6.2",
"typescript": "^2.6.2"

编辑:通过将返回类型更改为PromiseLike 解决。

【问题讨论】:

    标签: javascript typescript sequelize.js es6-promise bluebird


    【解决方案1】:

    你有没有试过这样设置:

    Sequelize.Promise = global.Promise
    

    【讨论】:

    • TypeScript 告诉我 Promise 属性是只读的。不过没问题,我把find方法的返回类型改成了PromiseLike
    【解决方案2】:

    我认为当前版本不可能做到这一点。该库似乎将 Promise 关键字替换为 Bluebird 库的一个实例。

    // @看这里 https://github.com/sequelize/sequelize/blob/fa587acc050b06db6ef6b69355f7d9350d6c01da/lib/promise.js#L3

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-09
      • 2015-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多