开发者

Is it possible to do non-blocking Facebook API calls, using node.js?

I want to use node.js to boost my Facebook applications performance. Imagine application that tries to compute who is you best friend on Facebook, fetching a lot of data from API calls - determining how many times your frien开发者_开发百科ds have posted to your wall, how many photos you have marked on together - so on.

So instead of running that calls one after one, as I do using PHP I have an idea to send them all together to Facebook using non-blocking, asynchronous nature of Node.js.

So overall execution time will be the time of most time consuming API call, but not the sum of execution time of the every call, right?

I try to use node-facebook-sdk (https://github.com/tenorviol/node-facebook-sdk) to make Facebook API calls and it seems to be that it's blocking, isn't it?

Just quick and dirty modification of example code, requesting 3 user profiles, seems that calls are not asynchronous, each sending to Facebook after previous has completed. Are there any way to avoid that?

Thank in advance!

var fbsdk = require('facebook-sdk');

var facebook = new fbsdk.Facebook({
  appId  : '_APPID_',
  secret : '_SECRET_'
});

var i = 0;

setInterval(function() {
  i++;
  console.log("TICK " + i);
}, 500);

facebook.api('/100000997949108', function(data) {
  console.log(data);
});

facebook.api('/1609464095', function(data) {
  console.log(data);
});

facebook.api('/100000560820400', function(data) {
  console.log(data);
});


This library will help you out with all things async. I hashed the particular method you would want to use for your problem, but the library as a whole is excellent at abstracting some of the more tedious (and ugly!) async patterns away. Great transitioning tool for those coming from procedural and you can take a peak under the covers if you want to learn some async patterns.

https://github.com/caolan/async/#parallel

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜