从异步函数返回一个值。 [英] Return a value from an asynchronous function.

查看:69
本文介绍了从异步函数返回一个值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





这就是我在js中的代码,



  var  dataset = testgettingdataset(sensorType,chart); 

function testgettingdataset(sensorType,chart){

var dataset = [];

var successCallback = function (data){
dataset = JSON .parse(data); // 数据集已定义
};

var checkdataset = dataset; // 数据集是一个空数组

返回数据集;
}





这里我想返回我的数据集变量。但是当我来到successcallback之外时,数据集是空的。



任何人都可以帮我这个吗?



我尝试了什么:



我试图在

  var  successCallback =  function (数据){
dataset = JSON .parse(数据); // 数据集已定义
};

解决方案

无法从 async 调用返回值的函数返回值。

它可以通过两种方式解决,要么你需要将ajax调用作为 async = false ,要么你必须在成功回调函数中编写与数据相关的代码

Hi,

This is what I have as my code in js,

var dataset = testgettingdataset(sensorType, chart);

    function testgettingdataset(sensorType, chart) {

        var dataset = [];

        var successCallback = function (data) {
            dataset = JSON.parse(data);  // dataset is defined
        };

        var checkdataset = dataset;   // dataset is an empty array

        return dataset;
    }



Here I want to return my dataset variable. But when I came outside the successcallback the dataset is empty.

Can anyone help me with this?

What I have tried:

I have tried to return the value even inside the

var successCallback = function (data) {
            dataset = JSON.parse(data);  // dataset is defined
        };

解决方案

Its not possible to return the value from an async call to the function returning value.
it can be solved in two ways, either you need to make the ajax call as async= false or you will have to write the data related code inside the success callback function.


这篇关于从异步函数返回一个值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆