javascript 使 FB.api() 调用同步
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5245610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make FB.api() calls synchronous
提问by Kevindra
I am creating fQuery API on top of FB javascript SDK. And till now everything worked fine, but i got stuck in FB.api calls now.
我正在 FB javascript SDK 之上创建 fQuery API。直到现在一切正常,但我现在陷入了 FB.api 调用。
Actually, I am trying to load facebook user object i.e. "/me" using FB.api function.
实际上,我正在尝试使用 FB.api 函数加载 facebook 用户对象,即“/me”。
function somefunc() {
var r = fQuery.load(selector); //selector = "me"
return r;
}
fQuery.load = function( selector ) {
fQuery.fn.response = "";
return FB.api( "/" + selector, function (response) {
// we get response here.
});
}
Is it possible to return the response or can we make it sync call. I have tried many ways to work around but could not get success.
是否可以返回响应,或者我们可以进行同步调用。我尝试了很多方法来解决,但都没有成功。
Please provide suggestions.
请提供建议。
回答by Martin Jespersen
If you think about it, you don't really want to make it synchronous. Javascript is single threaded by nature, making something that is asynchronous synchronous, would involve "freezing" the thread until the asynchronous call returns.
如果您考虑一下,您不会真的想让它同步。Javascript 本质上是单线程的,使异步同步的东西会涉及“冻结”线程,直到异步调用返回。
Even if you could do it, you don't want to, trust me.
即使你能做到,你也不想,相信我。
Redesign your code to work with the asynchronous nature instead of fighting it. you will create better applications, have happier users and become a better coder all at the same time.
重新设计您的代码以处理异步性质,而不是与之抗争。您将创建更好的应用程序,拥有更快乐的用户并同时成为更好的编码员。
回答by Alasdair McLeay
As commented elsewhere, making a synchronous call is useful if you want to open a popup after a successful response as browsers will often block popups that aren't a result of a direct user action.
正如其他地方所评论的那样,如果您想在成功响应后打开弹出窗口,则进行同步调用很有用,因为浏览器通常会阻止不是直接用户操作的结果的弹出窗口。
You can do this by manually calling the Open Graph API with JavaScript (or jQuery as per the example below) rather than using the Facebook JS SDK.
您可以通过使用 JavaScript(或按照下面的示例使用 jQuery)而不是使用 Facebook JS SDK 手动调用 Open Graph API 来完成此操作。
e.g. to upload a photo via the Open Graph API and then prompt the user to add it as their profile picture using a popup, without the popup being blocked:
例如,通过 Open Graph API 上传照片,然后提示用户使用弹出窗口将其添加为他们的个人资料图片,而不会阻止弹出窗口:
$.ajax({
type: 'POST',
url: 'https://graph.facebook.com/me/photos',
async: false,
data: {
access_token: '[accessToken]',//received via response.authResponse.accessToken after login
url: '[imageUrl]'
},
success: function(response) {
if (response && !response.error) {
window.open('http://www.facebook.com/photo.php?fbid=' + response.id + '&makeprofile=1');
}
}
});
回答by Salik
You probably want to call FB.api in a for loop iteration if this is the case you need its proper solution which exists in the use of Closures. Please read my answer here, given in another question
如果在这种情况下您需要使用Closures 时存在的正确解决方案,您可能希望在 for 循环迭代中调用 FB.api 。请在此处阅读我在另一个问题中给出的答案
回答by hey there
My solution was to make a recursive call until i got what i need from the FB.api
我的解决方案是进行递归调用,直到我从 FB.api 得到我需要的东西

