简体   繁体   English

为什么这总是返回“ undefined”

[英]Why does this always return “undefined”

The following code always returns "undefined" 以下代码始终返回“ undefined”

function sendCommand(cmdJson){
    chrome.extension.sendRequest(cmdJson, function(response){
        return response;
    });
}

I've also tried this variant with the same result 我也尝试过这种变体,结果相同

function sendCommand(cmdJson){
    var msg;
    chrome.extension.sendRequest(cmdJson, function(response){
        msg = response;
    });
    return msg;
}

If I do an alert(response); 如果我发出alert(response); instead of return response; 而不是return response; I get the expected value. 我得到了期望值。

I'm guessing that chrome.extension.sendRequest is asynchronous, in which case sendCommand doesn't return anything. 我猜chrome.extension.sendRequest是异步的,在这种情况下sendCommand不返回任何内容。 The response-handler inside sendCommand is the one that returns something, but that's not the same, as sendCommand returning something. sendCommand 的response-handler是一个返回内容的函数,但这与sendCommand返回某些内容不同。 So when you call sendCommand it returns undefined . 因此,当您调用sendCommand它将返回undefined

Basically, sendCommand invokes the chrome.extension.sendRequest function, and then returns undefined right away, while the chrome.extension.sendRequest function is still running. 基本上, sendCommand调用chrome.extension.sendRequest函数,然后在chrome.extension.sendRequest函数仍在运行时立即返回undefined

There's no real way to make something asynchronous behave synchronously - it's better to restructure your code. 没有真正的方法可以使异步行为同步执行-最好重组代码。

It is most likely because you are performing an ajax call, which is asynchronous by nature. 这很可能是因为您正在执行ajax调用,该调用本质上是异步的。 An asynchronous function cannot return a value since there is no way to know when the asynchronous call will complete. 异步函数无法返回值,因为无法知道异步调用何时完成。

If you are using jQuery, you have two alternative options to get around this problem. 如果使用的是jQuery,则有两个替代方法可以解决此问题。 If you are using jQuery 1.5 or later, then you can use Deferred Objects. 如果您使用的是jQuery 1.5或更高版本,则可以使用Deferred Objects。 Deferred Objects are a new object that jQuery allows you to use to interact with asynchronous code. 延迟对象是jQuery允许您与异步代码进行交互的新对象。

By default, a jquery ajax function (ie $.ajax, $.get, $.post, $.getJSON) returns a Deferred Object, so all you have to do is store your ajax call in a variable and then call available methods listed in the jQuery Deferred Object api. 默认情况下,jQuery ajax函数(即$ .ajax,$。get,$。post,$。getJSON)返回Deferred Object,因此您要做的就是将ajax调用存储在变量中,然后调用列出的可用方法在jQuery Deferred Object api中。 Here is one of the best tutorials I have seen on jQuery Deferred Objects. 这是我在jQuery Deferred Objects上看到的最好的教程之一。 http://www.erichynds.com/jquery/using-deferreds-in-jquery/ http://www.erichynds.com/jquery/using-deferreds-in-jquery/

var sendResponse = $.get("/getResponse");

//Somewhere else in your code
sendResponse.success(function(response) {
    return response;
});

The other options is to make your jquery ajax call synchronous. 其他选项是使您的jquery ajax调用同步。 You can do this by setting the async ajax property to false . 您可以通过将async ajax属性设置为false来实现

$.ajax({
    url: "someURL",
    async: false
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM