在下面的代码中,我希望在调用a, b, c
时processhttprequest()
将变量作为参数传递。
var q = require("q");
var request = require('request');
function myfun()
{
var a, b, c;
//do some work here
var httprequest = q.denodeify(request);
var httprequestpromise = httprequest(httpoptions);
httprequestpromise.then(processhttprequest);
}
我尝试过httprequestpromise.then(processhttprequest.bind([a, b, c]));
但没有运气。Q或任何其他Promise库是否支持此功能。
您可以这样使用.bind()
:
httprequestpromise.then(processhttprequest.bind(null, a, b, c));
这将创建一个虚拟函数,将增加的参数a
,b
和c
之前调用processhttprequest()
。
或者,您可以使用自己的存根函数手动执行以下操作:
function myfun()
{
var a, b, c;
//do some work here
var httprequest = q.denodeify(request);
var httprequestpromise = httprequest(httpoptions);
httprequestpromise.then(function(result) {
return processhttprequest(a, b, c, result);
});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句