无法获得承诺工作,后续承诺未被调用

时间:2016-07-18 16:29:58

标签: javascript node.js ecmascript-6 es6-promise

我正在尝试使用其他承诺扩展一些现有代码,但它们对我来说是一个新主题,我显然错过了一些东西。这是作为npm的构建脚本的一部分运行。

我目前正在努力实现的最终目标是在每个架构发生打包操作之后调用。我试过把它包装成

return new Promise

但目前我没有从该功能返回任何内容,所以我不确定我应该在最后的解决方案中包含什么。如果我只是调用解决方案,并且没有任何事情发生,并将它包装在一个promise中似乎导致该函数实际上没有运行,并且没有错误被捕获到任何地方?

我猜这是完全错误的,我想要实现的是在前一个功能完成后运行另一个功能吗?

这是代码,因为它代表附加。然后我无法被调用。

function build(cfg) {
  return new Promise((resolve, reject) => {
    webpack(cfg, (err, stats) => {
      if (err) return reject(err);
      resolve(stats);
    });
  });
}

function startPack() {
  console.log('start pack...');
  build(electronCfg)
    .then(() => build(cfg))
    .then(() => del('release'))
    .then(paths => {
      if (shouldBuildAll) {
        // build for all platforms
        const archs = ['ia32', 'x64'];
        const platforms = ['linux', 'win32', 'darwin'];

        platforms.forEach(plat => {
          archs.forEach(arch => {
            pack(plat, arch, log(plat, arch));
          });
        });
      } else {
        // build for current platform only
        pack(os.platform(), os.arch(), log(os.platform(), os.arch()));
      }
    })
    .then(() => {
      console.log('then!');
    })
    .catch(err => {
      console.error(err);
    });
}

function pack(plat, arch, cb) {
  // there is no darwin ia32 electron
  if (plat === 'darwin' && arch === 'ia32') return;

  const iconObj = {
    icon: DEFAULT_OPTS.icon + (() => {
      let extension = '.png';
      if (plat === 'darwin') {
        extension = '.icns';
      } else if (plat === 'win32') {
        extension = '.ico';
      }
      return extension;
    })()
  };

  const opts = Object.assign({}, DEFAULT_OPTS, iconObj, {
    platform: plat,
    arch,
    prune: true,
    'app-version': pkg.version || DEFAULT_OPTS.version,
    out: `release/${plat}-${arch}`,
    'osx-sign': true
  });

  packager(opts, cb);
}

1 个答案:

答案 0 :(得分:0)

你没有说Public Sub employee_collection() ' this collection saves all of the employees records Dim employee As Collection Set employee = New Collection Dim n As Integer Dim i As Integer Dim E1 As Variant Dim j As Integer n = 528 Dim a, b As String For i = 3 To n a = "A" + CStr(i) ' to get the values from the excel sheet b = "B" + CStr(i) Set E1 = New clsEmployee E1.ID = Sheets("A").Range(a).Value ' save the valus of each employee in the collection E1.Age = Sheets("A").Range(b).Value employee.Add E1 Next i End Sub 是什么,但是如果它是一个普通的日志记录功能,那么看起来你正在传递log(调用undefined的结果) log(...)的{​​{1}}参数。也许你的意思是:

cb

在任何情况下,这都不会等待包装完成。我不知道为什么你没有看到任何控制台输出,但是如果你想要在所有打包完成后发生这个输出,那么你需要将pack包装在一个promise中。类似的东西:

pack(plat, arch, () => log(plat, arch));

然后使用packager而不是var pack = (plat, arch) => new Promise(resolve => { // ... packager(opts, resolve); }); 来执行所有打包(如果可以的话,并行执行):

Promise.all