将parameter passing给Promise Sequence

我正在使用一个节点js插件: https : //www.npmjs.com/package/promise-sequence ,我想要做的是在调用pipe道时将parameter passing给每个函数。

var Nightmare = require('nightmare'); var pipeline = require('promise-sequence/lib/pipeline'); var commonFunctions = require('./websites/_common/commonFunctions') var nightmare = Nightmare({ show: true, fullscreen : true, waitTimeout: 10000 }); var resultsPromise = pipeline([ commonFunctions.accessURL(nightmare), commonFunctions.loginToWebsite(nightmare), ]) .then(() => commonFunctions.success(nightmare)) .catch((error) => console.log(error)); 

但是,当我试图传递参数时,它给了我一个错误:

 TypeError: tasks[0].apply is not a function at C:\sad\node_modules\promise-sequence\lib\pipeline.js:25:57 at process._tickCallback (internal/process/next_tick.js:103:7) at Module.runMain (module.js:607:11) at run (bootstrap_node.js:418:7) at startup (bootstrap_node.js:139:9) at bootstrap_node.js:533:3 

我怎样才能把我的梦魇variables作为pipe道中的parameter passing给每个函数?

您可以绑定这些function:

 var resultsPromise = pipeline([ commonFunctions.accessURL.bind(null, nightmare), commonFunctions.loginToWebsite.bind(null, nightmare), ])... 

或者使用匿名函数:

 var resultsPromise = pipeline([ function () { return commonFunctions.accessURL(nightmare); }), function () { return commonFunctions.loginToWebsite(nightmare); }), ])... 

如果您使用ES6,您可以使用箭头function缩短它:

 var resultsPromise = pipeline([ () => commonFunctions.accessURL(nightmare), () => commonFunctions.loginToWebsite(nightmare), ])... 

这里需要注意的是,pipeline需要传递函数数组,通过这些方法,我们保留了传递函数,但是commonFunctions.accessURLcommonFunctions.loginToWebsite也会被nightmarevariables调用。

你的代码不起作用,或者你说直接调用它的原因是,当你调用你的函数,他们开始执行,他们返回的承诺,但pipe道不期望承诺,而是期望返回承诺的函数,所以它会在开始执行时调用这些函数。 绑定基本上创build了预先加载给定参数的新函数,这就是我们在匿名函数中所做的事情。