同步 2 个 facebook api 调用的结果

Synchronize results of 2 facebook api calls

我有一个 table,我想从我的 JS 中填充它。用于填充 table 的数据通过以下调用获得:

for(var i = 0; i < arr.length; ++i) {
    FB.api(arr[i].id, OnEventResponse);
    FB.api(arr[i].id + '/picture', {"redirect": false,"type": "large"}, OnPictureResponse);
}

因为它处于 for 循环中,所以我无法在 OnPictureResponseOnEventResponse 上异步填充 table。函数 OnPictureResponse 的响应时间较长,我无法在 <tr> 标记上设置 id,因为函数 OnPictureResponse 中的响应不包含它...
有什么办法可以解决这个问题吗?我不知道.. 也许回调会给我来自两个 API 调用的响应。

解决这个问题的一个好方法是使用承诺:https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Promise

我更喜欢使用这个 polyfill:https://github.com/jakearchibald/es6-promise

...因为一旦我可以停止支持旧版浏览器,我唯一需要做的就是删除 polyfill。

另一种(可能更好)方法是在 Facebook 中使用批处理请求。您可以在文档中找到相关信息:https://developers.facebook.com/docs/graph-api/making-multiple-requests