Meteor 如何使用 Meteor.wrapAsync with facebook Graph Api
Meteor how to use Meteor.wrapAsync with facebook Graph Api
我通过使用 fibers/future 解决了 Graph Api 异步请求,它允许在预定义的时间后给出函数结果,这个解决方案的缺点是当 facebook 发送响应快于 1000 毫秒时它会等待无论如何。
有什么方法可以让服务器端函数在响应到来后立即生成 returns 图 api 结果?我发现 Meteor.wrapAsync 可能会有帮助,但我不确定我是否正确理解了它的语法。
这是我使用光纤所做的,它正好工作了一秒钟。
function graphGet(query){
var response = new Future(); // wait for async FB response
var waitingTime = 1000;
var graphResponse = "no result after: " + waitingTime + "ms";
FBGraph.get(query, function(error, response) {
if (response) { graphResponse = response; }
else { graphResponse = error; }
});
setTimeout(function() {
response['return'](graphResponse);
}, waitingTime);
return response.wait();
}
使用 Meteor.wrapAsync
的相同代码要短得多:
function graphGet(query){
// wrap the async func into a FBGraph bound sync version
var fbGraphGetSync = Meteor.wrapAsync(FBGraph.get, FBGraph);
// use a try / catch block to differentiate between error and success
try{
var result = fbGraphGetSync(query);
return result;
}
catch(exception){
console.log(exception);
}
}
我通过使用 fibers/future 解决了 Graph Api 异步请求,它允许在预定义的时间后给出函数结果,这个解决方案的缺点是当 facebook 发送响应快于 1000 毫秒时它会等待无论如何。
有什么方法可以让服务器端函数在响应到来后立即生成 returns 图 api 结果?我发现 Meteor.wrapAsync 可能会有帮助,但我不确定我是否正确理解了它的语法。
这是我使用光纤所做的,它正好工作了一秒钟。
function graphGet(query){
var response = new Future(); // wait for async FB response
var waitingTime = 1000;
var graphResponse = "no result after: " + waitingTime + "ms";
FBGraph.get(query, function(error, response) {
if (response) { graphResponse = response; }
else { graphResponse = error; }
});
setTimeout(function() {
response['return'](graphResponse);
}, waitingTime);
return response.wait();
}
使用 Meteor.wrapAsync
的相同代码要短得多:
function graphGet(query){
// wrap the async func into a FBGraph bound sync version
var fbGraphGetSync = Meteor.wrapAsync(FBGraph.get, FBGraph);
// use a try / catch block to differentiate between error and success
try{
var result = fbGraphGetSync(query);
return result;
}
catch(exception){
console.log(exception);
}
}