Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

This is what I have as my code in js,

JavaScript
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
JavaScript
var successCallback = function (data) {
            dataset = JSON.parse(data);  // dataset is defined
        };
Posted
Updated 21-Mar-17 20:24pm
Comments
Karthik_Mahalingam 22-Mar-17 2:04am    
are you using jquery ajax ?

1 solution

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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900