Javascript 回调 - 如何返回结果?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6453295/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:48:20  来源:igfitidea点击:

Javascript callback - how to return the result?

javascriptcallback

提问by Pjn2020

I am struggling to totally understand callbacks and i am stumbling at the final hurdle.

我正在努力完全理解回调,我在最后的障碍中跌跌撞撞。

Within JS I am calling a function which then calls a PHP function using a dojo rpc Json Service. I have stepped through the function in firebug and the PHP is executing and returning me the correct response via the callback but I don't know how to return the value to the initial JS variable that invoked the JS function? E.g.

在 JS 中,我调用一个函数,然后使用 dojo rpc Json 服务调用 PHP 函数。我已经遍历了 firebug 中的函数,PHP 正在执行并通过回调向我返回正确的响应,但我不知道如何将值返回到调用 JS 函数的初始 JS 变量?例如

JS Function 1

Function one(){

Var test = getPhp(number);

}

function getPhp(number)
{

this.serviceBroker = new dojo.rpc.JsonService(baseUrl + '/index/json-rpc/');

    var result = serviceBroker.phpFunc(number);

    result.addCallback(
        function (response)
        {
            if (response.result == 'success')
            {
                return response.description;
               //I am trying to pass this value back to the 
               //var test value in   function one

            }
        }
    );
}

Basically i now need to pass response.description back to my var test variable in function one.

基本上我现在需要将 response.description 传递回函数一中的 var 测试变量。

Any help is appreciated

任何帮助表示赞赏

回答by Kamiel Wanrooij

This is not possible, since the callback is run asynchronously. This means that the getPhp function returns before the callback is executed (this is the definition of a callback, and one of the reasons asynchronous programming is hard ;-) ).

这是不可能的,因为回调是异步运行的。这意味着 getPhp 函数在执行回调之前返回(这是回调的定义,也是异步编程很难的原因之一 ;-) )。

What you want to do is create a new method that uses the testvariable. You need to call this method when the callback is executed.

您想要做的是创建一个使用该test变量的新方法。执行回调时需要调用此方法。

i.e.

IE

function one(result) {
  var test = result;
  // Do anything you like
}

function getPhp(number, callback) {
  this.serviceBroker = new dojo.rpc.JsonService(baseUrl + '/index/json-rpc/');
  result.addCallback(
    function (response)
    {
        if (response.result == 'success')
        {
           callback(response.description);
        }
    }
  );
}

getPhp(number, function(result) { one(result); });

This last method creates an 'anonymous function' that is passed to the getPhp function. This function gets executed at the time the response arrives. This way you can pass data to the one(number)function afterthe data arrives.

最后一个方法创建了一个传递给 getPhp 函数的“匿名函数”。该函数在响应到达时执行。这样你就可以在数据到达后将数据传递给one(number)函数。

回答by Quentin

The short answer is that you can't.

简短的回答是你不能。

Do whatever you want to do with the data in the callback or functions you call from the callback. You can't return anything from it.

对回调中的数据或从回调中调用的函数执行任何您想做的操作。你不能从中返回任何东西。

回答by Florian Courtial

A much cleaner answer:

一个更干净的答案:

getPhp(number);

function one(data){
    var test = data;
    // do what you want
}

function getPhp(number)
{

    this.serviceBroker = new dojo.rpc.JsonService(baseUrl + '/index/json-rpc/');

    var result = serviceBroker.phpFunc(number);

    result.addCallback(
        function (response)
        {
            if (response.result == 'success')
            {
                one(response.description);
            }
        }
    );
}