由于链,在同步操作中使用承诺

Using promises in sync operation because of chain

我有这条链:

phase_one(files).then(phase_two).then(phase_three).then(phase_four);

每个阶段都会收到这个包含目标文件的数组。在每个阶段中,我都有这段默认代码:

  return Promise.map(files, function(file) {
    // some code here
  }, {
    concurrency: 3000
  });

问题是..phase_two 是同步的...但我仍想使用链我不应该使用 Promise.map.

我想知道在 bluebird 中是否有什么可以帮助我解决这个问题。我在文档中搜索但没有找到。

谢谢。

phase_two 可以只是一个 return 值的函数。该链条将继续正常运行。

phase_one(files).then(phase_two).then(phase_three).then(phase_four);

function phase_two(input) {
    // do something
    return result;
}

.then() 处理程序可以是同步的也可以是异步的。如果它是异步的,那么它必须 return 一个承诺。如果它是同步的,它可以 return 一个值成为传递给链的下一部分的值。


链中的第一个 link 必须 return 承诺(启动链)。如果 phase_one() 是同步的并且您想保持以前的结构,那么您可以从 phase_one() 中直接 return Promise.resolve(data);。这将使 return 值成为一个具有 .then() 处理程序的承诺。