node.js promise-sequene在每个任务之间运行一个函数

我使用的是promise-sequence/lib/pipeline ,我有一个基本的pipe道:

  var resultsPromise = pipeline([ commonFunctions.accessURL(nightmare), commonFunctions.loginToWebsite(nightmare), ]) .then(() => commonFunctions.success(nightmare)) .catch((error) => console.log(error)); 

.catch()将在最后运行, .catch()将在错误情况下运行。

我需要的是在pipe道中启动每个函数之前或在pipe道中的每个函数之后运行一个名为commonFunctions.recordLog()的函数。

我怎样才能做到这一点?

编辑

为了确保你明白我想达到的目的,下面是一个例子:

 var resultsPromise = pipeline([ commonFunctions.accessURL(nightmare), commonFunctions.recordLog() commonFunctions.loginToWebsite(nightmare), commonFunctions.recordLog() commonFunctions.loginToWebsite(nightmare), commonFunctions.recordLog() commonFunctions.loginToWebsite(nightmare), commonFunctions.recordLog() ]) .then(() => commonFunctions.success(nightmare)) .catch((error) => console.log(error)); 

这将工作,但这不是最好的解决scheme。

pipe道将前一个任务的结果作为parameter passing给下一个任务。

因此,不要打破计算,我们需要将commonFunctions.recordLog()调用包装到一个接受参数的函数中,然后调用recordLog并返回它的参数而不做任何修改。

如果commonFunctions.recordLog()是asynchronous的,并返回一个承诺包装将看起来像这样:

 x => commonFunctions.recordLog().then(() => x) 

正如你所看到的,用'x'来解决recordLog返回的问题。

如果commonFunctions是同步的,你可以简单地做:

 x => { commonFunctions.recordLog(); return x; } 

另外我会写一个函数来自动插入这些额外的任务,如下所示:

 let addLogTasks = function (tasks) { let tasksWithLogging = []; for (let task of tasks) { tasksWithLogging.push(task, x => commonFunctions.recordLog().then(() => x)); } return tasksWithLogging; }; //And then pipeline(addLogTasks(pipelineTasks)) .then((x) => console.log(x)) .catch((error) => console.log(error));