webpack().run()如何同步执行?

问题

const compiler1 = webpack(config)
const compiler2 = webpack(config2)

const build = async () => {
    await compiler1.run()
    console.log('continue...')
    await compiler2.run()
}
# 预期
compiler1.run()执行
compiler1.run()执行完成
输出 continue...
compiler2.run()执行
...
# 实际
输出 continue...
compiler1.run()执行
compiler1.run()完成
compiler2.run()执行
...

怎么可以得到我的预期结果,通过async await方式

阅读 4.3k
1 个回答

需要进行包装.

function runWebpack(config) {
    return new Promise((resovle, reject) => {
        return webpack(config).run((err, stats) => {
            return err ? reject(err) : resolve(stats);
        });
    });
}

(async function() {
    await runWebpack(config1);
    await runWebpack(config2);
})();
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题