当前位置: 首页 > 面试题库 >

等待API调用以Javascript完成后再继续

蔚楷
2023-03-14
问题内容

我过去一直在努力,今天一直在努力的事情是阻止API / AJAX继续进行,直到您收到响应为止。目前,我正在使用Facebook
API。我需要从调用中获取响应,然后将其返回,但是正在发生的事情是,在我从未从API调用中获取响应之前,我的函数正在返回。我知道为什么会这样,我只是想不出如何预防!这是我的代码…

function makeCall(){

var finalresponse = "";
    var body = 'Test post';

      FB.api('/me/feed', 'post', { message: body }, function(response) {
        if (!response || response.error) {
         finalresponse = response.error;
        } else {
          finalresponse = 'Post ID: ' + response.id;
        }
      });
      return finalresponse;

}

// -----编辑

我注意到有人建议这样的事情…

function makeCall(){
var finalresponse = "";
FB.api('/me/feed', 'post', { message: body }, function(response) {
        if (!response || response.error) {
         finalresponse = response.error;
         return finalresponse;
        } else {
          finalresponse = 'Post ID: ' + response.id;
          return finalresponse;
        }
      });
}

但这返回未定义

//根据更新进行编辑

function share_share(action_id){

  var finalresponse = makeCall(action_id, process);
  return finalresponse;

}

function makeCall(action_id, callback){

var body = 'Test post';
      FB.api('/me/feed', 'post', { message: body }, function (response) {
        if (!response || response.error) {
         var finalresponse = response.error;
        } else {
          finalresponse = 'Post ID: ' + response.id;
        }
        callback(action_id, finalresponse);
      });

}
function process(action_id, finalresponse){
  console.log(finalresponse);
}

问题答案:

每天要问100次并且似乎不可能有一个答案的问题。

调用是异步的,因此不可能一步完成。您期望的基本示例。

function one() {
  var a = 1;
  return a;
}
alert( one() );

实际发生了什么:

function one() {
  var a;
  window.setTimeout( 
     function() {
        a = 1;
     }, 2000);
  return a;  //This line does not wait for the asynchronous call [setTimeout/ajax call] to run. It just goes!
}
alert( one() );

您需要做的是将其分为两部分。

function one( callback ) {   
   window.setTimeout( function(){  //acting like this is an Ajax call
        var a = 1;
        callback(a);
   },2000);
}
function two( response ) {
   alert(response);
}
one( two );

因此,在您的情况下,您需要分解代码以分两个部分来处理它。

function makeCall( callback ) {
    var body = 'Test post';
      FB.api('/me/feed', 'post', { message: body }, function (response) {
        if (!response || response.error) {
         var finalresponse = response.error;
        } else {
          finalresponse = 'Post ID: ' + response.id;
        }
        callback(finalresponse);
      });
}


function processResponse( response ) {
    console.log(response);
}
makeCall(processResponse);


 类似资料:
  • 问题内容: 为什么每当我将ajax放入for循环中时,它都无法很好地同步? 例如,我的代码是: 为什么它先调用Ajax查询?是否有可能让ajax查询在继续之前完成?因为它在完成填充之前就清除了数组。:/ 问题答案: 首先,您确实需要了解Ajax调用是如何异步的(这就是Ajax中的“ A”所代表的意思)。这意味着调用仅启动ajax调用(它将请求发送到服务器),其余代码愉快地继续运行。有时,在其余代码

  • 我正在使用JavaScript处理图像。对于每个图像,我使用.toblob创建4个文件。问题在于.toblob是异步的,这样一个.toblob进程可以在其他进程完成时运行。await异步似乎不起作用。处理的图像数的计数器似乎不起作用,因为上一个图像可以处理,而前一个图像尚未完成。最后一个图像将计数器增加到图像数并触发保存。 我可以在最后一个图像上做一个setTimeout,但那只是在猜测最大时间。

  • 问题内容: 我的document.ready()中有一些ajax调用 喜欢 : 我如何强制它等待,直到我们从ajax请求获得所有回叫之前不继续? 问题答案: 我根本不喜欢任何答案,最好的方法(自Jquery 1.5+起)是使用Deferred对象,这些是操纵异步调用的对象,可以解决: 这样,myFunc会在执行2个ajax调用之后执行,如果其中一个出错,则执行myFailure。 您可以在jque

  • 有什么想法吗?没有JS库。 编辑:我做了几次回拨尝试。老实说,我很难理解他们。下面是我的最后一次尝试:

  • 问题内容: 怎么可能 等到动作完成后再继续循环,我不能得到$ this var,因为它具有最后一个值,对不起我的英语,谢谢!!! 问题答案: 选项1:切换到处理程序中数组中的下一个元素。 选项2:同步发出Ajax请求: 全球: 或直接在请求中: });

  • 问题内容: 基本上,一旦用户在我的应用程序中离开了网页,我就需要使用AJAX调用PHP脚本,这会将在网页上花费的时间插入数据库中,然后离开该页面。 重要的是要等待AJAX​​请求完成,因为用户无法访问我的应用程序中的网页,除非用户在前一页上花费了一定的时间(例如两分钟)。 这是我的jQuery代码: 我应该如何更改它,以便它将在离开网页之前等待AJAX​​请求结束? 编辑: 或者,最好每隔一分钟左