I am attempting to populate a global variable using a function and then call subsequent functions after the data is retrieved. Below is the current code I have:
$(function () {
var jsonData = {};
var dataRetrievalPromise = function () {
return new Promise(function (resolve, reject) {
jsonData = { "data": [{ "test": "value1" }, { "test2": "value2" }, { "test3": "value3" }] }
return resolve(jsonData);
});
}
dataRetrievalPromise().then(function (result) {
generateTabs();
});
});
function generateTabs() {
var test = jsonData;
};
Although dataRetrievalPromise successfully sets the value of jsonData, I encounter an issue when trying to access it within generateTabs(), as it appears undefined.
Edit: I neglected to include the resolve statement. Edit 2: Post punctuation has been corrected, however, the problem persists in my code.
Following Adiga's suggestion, I replaced dataRetrievalPromise with the below code, but unfortunately, the behavior remains the same.
var dataRetrievalpromise = function () {
return new Promise(function (resolve, reject) {
return resolve(jsonData = { "data": [{ "test": "value1" }, { "test2": "value2" }, { "test3": "value3" }] });
});
};