是否可以使用 node.js 进行非阻塞 Facebook API 调用?
我想使用 node.js 来提高我的 Facebook 应用程序性能。想象一下应用程序试图计算谁是你在 Facebook 上最好的朋友,从 API 调用中获取大量数据 - 确定你的朋友在你的墙上发布了多少次,你们一起标记了多少张照片 - 等等。
因此,我不想像使用 PHP 那样一个接一个地运行调用,而是使用 Node.js 的非阻塞、异步特性将它们全部发送到 Facebook。
那么总体执行时间将是最耗时的 API 调用的时间,而不是每个调用的执行时间之和,对吗?
我尝试使用node-facebook-sdk(https://github.com/tenorviol/node-facebook -sdk)来进行 Facebook API 调用,但它似乎被阻止了,不是吗?
只是快速而肮脏地修改示例代码,请求 3 个用户配置文件,似乎调用不是异步的,每个调用都在前一个完成后发送到 Facebook。有什么办法可以避免这种情况吗?
预先感谢!
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);
});
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 friends 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);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个库将帮助您解决所有异步问题。我散列了您想要用于解决问题的特定方法,但该库作为一个整体非常擅长抽象出一些更乏味(而且丑陋!)的异步模式。对于那些来自程序的人来说这是一个很棒的转换工具,如果你想学习一些异步模式,你可以在幕后进行深入研究。
https://github.com/caolan/async/#parallel
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