Webpack 编译器实例承诺?

Webpack compiler instance promisification?

我正在使用 Webpack 2 Node API and I would like to promisify the run() method using bluebird

import Promise from 'bluebird'
import webpack from 'webpack'

const compiler = webpack(config)
const runAsync = Promise.promisify(compiler.run)

runAsync().then(stats => {
  console.log('stats:', stats)
}).catch(err => {
  console.log('err:', err)
})

我得到的错误是:

[TypeError: self.applyPluginsAsync is not a function]

所以我猜测 webpack 代码的编写方式与 bluebird promisification 不兼容。

如果有任何其他方式来 promisify webpack 的 run() 方法..?

所有这些回调和 if 语句都让我很烦。

您需要将 compiler 作为上下文传递给 promisify 方法。

const runAsync = Promise.promisify(compiler.run, { context: compiler });

或者这样称呼:

runAsync.call(compiler).then(stats => {...

来自蓝鸟 Docs:

Note that if the node function is a method of some object, you can pass the object as the second argument like so:

var redisGet = Promise.promisify(redisClient.get, {context: redisClient});
redisGet('foo').then(function() {
    //...
});