【问题标题】:Babel transform-async-to-module-method to Bluebird with ES6 mapsBabel transform-async-to-module-method to Bluebird with ES6 maps
【发布时间】:2016-09-13 06:56:30
【问题描述】:

我们正在尝试将 Node.js 6.5.0 与 Babel 一起使用,以使 async functions 使用 Bluebird 而不是原生 V8 ES6 承诺:

我们的package.json 仅包含以下Babel 条目:

"devDependencies": {
  "babel-cli": "^6.9.0",
  "babel-plugin-transform-async-to-module-method": "^6.8.0",
  "babel-plugin-transform-es2015-destructuring": "^6.9.0",
  "babel-plugin-transform-es2015-modules-commonjs": "^6.14.0",
}

.babelrc:

{
  "plugins": [
    "transform-es2015-modules-commonjs",
    "transform-es2015-destructuring",
    [
      "transform-async-to-module-method",
      {
        "module": "bluebird",
        "method": "coroutine"
      }
    ]
  ]
}

然而我们async functions返回的ES6映射在执行过程中会导致如下错误:

产生了一个不能被视为承诺的值 [object Map]

我们如何解决这个问题?

附:当async functions 使用transform-async-to-generator 转换为generators 时,一切正常

【问题讨论】:

    标签: node.js ecmascript-6 babeljs bluebird


    【解决方案1】:

    下面是一些触发相同错误的示例代码:

    function giveMap() {
      return new Map();
    }
    
    void async function() {
      await giveMap();
    }();
    

    请注意,giveMap 未标记为 async(这是实际问题)。

    此代码将在使用 transform-async-to-generator 时运行,因为 Map 可以从生成器中生成:

    function* () {
      yield new Map();
    }
    

    但是,当使用transform-async-to-module-method时,我认为代码变得类似于这样:

    Promise.coroutine(function* () {
      yield new Map();
    });
    

    这将导致错误,as explained here,因为Promise.coroutine() 期望得到承诺。

    因此,您应该留意返回 Map、已启用 await 但未映射 async 的函数。

    【讨论】:

    • 谢谢。完全有道理。我们会进行调查并通知您。
    猜你喜欢
    • 2018-11-27
    • 2020-02-21
    • 2018-03-20
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 2022-12-02
    • 1970-01-01
    • 2022-12-27
    相关资源
    最近更新 更多