javascript – 如何将参数传递给Node的Q库(denodeify)承诺处理程序
作者:互联网
在下面的代码中,我希望变量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或任何其他承诺库支持.
解决方法:
您可以像这样使用.bind():
httprequestpromise.then(processhttprequest.bind(null, a, b, c));
这将创建一个虚函数,在调用processhttprequest()之前将添加参数a,b和c.
或者,您可以使用您自己的存根函数手动执行此操作:
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);
});
}
标签:q,javascript,node-js,promise 来源: https://codeday.me/bug/20190830/1765440.html