怎样散布? [英] How to promisify?
本文介绍了怎样散布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这种功能:
someFunction.someMethod('param1', function(err, res1, res2) {
req.method(res1, function(err) {
if (!err) {
console.log('Yes!');
}
});
})(req, res); // <-- This one's the problem!
现在,当我尝试传播它时:
Now when I try to promisify it:
var a = Promise.promisify(someFunction.someMethod);
a('param1').spread(function(res1, res2) {
console.log('Yes!');
}).catch(function(err) {
});
它不再工作了,因为我不能在其末尾放置(req,res)
.如何实现呢?
it doesn't work anymore, because I cannot put the (req, res)
at the end of it. How to achieve this?
推荐答案
我会尝试这样做:
var a = Promise.promisify(function(arg, cb) {
someFunction.someMethod(arg, cb)(req, res);
});
a('param1').spread(function(res1, res2) {
console.log('Yes!');
}).catch(function(err) {
});
...是的,这很丑.但是,此部分应用程序可能表明 someFunction.someMethod('param1',…)
返回的函数应被多次调用;并且您的回调将被调用多次-您将无法再使用promises.
…and yes, it's ugly. This partial application might however be a sign that the function returned by someFunction.someMethod('param1', …)
is supposed to be called multiple times; and your callback would be called as many times - where you cannot use promises any more.
这篇关于怎样散布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文