JavaScript - Problem getting data from promise object [duplicate] - javascript

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 1 year ago.
So, I'm new to using promise object... I'm trying to make a Facebook instant game which uses the function FBInstant.player.getDataAsync().then(). This function returns a promise and I have no clue how to use the data returned other than with console.log(). If I try to insert the data to a defined variable it becomes unidentified.
Example:
User = [];
FBInstant.player.getDataAsync(['name']).then(function(data) {
User['name'] = data['name'];
});
console.log(User['name']) // return unidentified.
Sorry for my poor explanation I'm new to this and I'm just trying to accomplish a simple task.

User = [];
FBInstant.player.getDataAsync(['name']).then(function(data) {
User['name'] = data['name'];
console.log(User['name'])
});
This should give you a response

Related

.then cannot populate array javascript [duplicate]

This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 months ago.
I have problem with this function, it returns a console log, but does not push the data to the array, how can I fix this, I know the problem is related to promises but I don't know how I can fix it.
// Parse XLSX DATA function
function parseXLSXData(myFile) {
// Using the Read XLSX file cdn JS
let parseData = [];
readXlsxFile(myFile).then((rows) => {
// `rows` is an array of rows
// each row being an array of cells.
console.log(rows);
parseData.push(rows);
});
return parseData;
}

how to call a variable defined below it in Javascript [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
i have the following code and i'm trying to console log a variable called trt i created below it: how can i get trt in console log??
thanks
this.rows = this.dataService.SetItem('added-items',());{
console.log(trt);
this.dataService.GetJson().subscribe((result)=>
{
this.rows=(JSON.parse(result['_body']));
var trt=(result['_body']);
//this.rows = this.dataService.SetItem("data",this.rows);
})
thanks!!
You need to log it to the console after you define it.
this.rows = this.dataService.SetItem('added-items',());{
//console.log(trt);
this.dataService.GetJson().subscribe((result)=>
{
this.rows=(JSON.parse(result['_body']));
var trt=(result['_body']);
console.log(trt);
//this.rows = this.dataService.SetItem("data",this.rows);
})
Edit: you cannot call a variable that has not been defined yet and expect it to not be undefined.

Make return call from nested function [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
function runimage(){
var vel = val.id;
var secondlink = 'someurl'+vel+'stuff';
$.getJSON(secondlink, function(beyta){
var target = beyta.attachments.data[0].media.image.src;
return target;
});
return "FISH";
}
I am making an API call via jQuery's getJSON method. The problem I am having is getting the runimage() function return the value of my target value.
The code return target gives me undefined. This is the result even if I place a dummy string in place of target.
To see if this is a problem with closures, I added a dummy return "FISH" outside of my getJSON call, and the code does indeed return "FISH"
How would I go about getting runimage() to return the value of variable target?
The issue is that while you are waiting to get your AJAX response, the function has already returned "FISH".

global variable in javascript not working; loses data [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
var feed;
$.getJSON('short.json', function(data) {
feed = data.items;
console.log(feed);
});
console.log(feed);
I have this short code written above. I am expecting feed to be a global variable but once it comes out the function, it's undefined again. It prints out an object when inside. What am I doing wrong?
Thanks for the help.
The reason is that the getJSON() call is asynchronous. It won't run until AFTER the second console.log();

JavaScript function returns empty string when a value is expected [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
There's probably an obvious mistake somewhere, but I just can't out find what's wrong.
I'm trying to retrieve data from an api, and use the js-function below to get it. If I do an alert(key) on the data inside the $.get-function, it alerts the correct data.
function getApiKey(company, password) {
var url = "http://myapi.com/" +company+ "?password=" +password;
var key = "";
$.get(url).done(function(data) {
key = data;
//alert(key) returns the correct data
});
return key;
}
However, I need to use the function in a different file, and that's where it doesn't work. When I do
var key = getApiKey("company", "password");
alert(key);
key is empty.
The $.get command is asynchronous, meaning that your function returns key with its initial value of "", then later when the async callback fires, it runs key=data.
You should read up on asynchronous behaviour in javascript - for a solution, you'll need some way to get the data back to the place of the call asynchronously. One such option is a promise.

Categories

Resources