Javascript - var values not changed inside callback? [duplicate] - javascript

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 1 year ago.
Why does changing the values of status and output inside the callback of a function I await for doesn't change their values outside?
var status = '';
var output = '';
if (orderTranscription != "TRANSCRIPTION_ERROR") {
await runPython38Script ("processing.py", orderTranscription, (output) => {
const orderInfo = JSON.parse(output);
// Both of these have a value defined here when I console.log, not empty string
status = orderInfo.status ? "VALID" : "PROCESSING_ERROR";
output = orderInfo.output;
});
}
// When I try to console.log outside, I get empty string.
console.log(status);
console.log(output);

Related

This function is returning undefined and I want it to return the value of the variable final. How can I achieve this? [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 4 months ago.
This function is returning undefined and I want it to return the variable final. How can I achieve this? Please help!!
const getIndex = ()=>{
let heroName = searchfield.value
fetch(`https://akabab.github.io/superhero-api/api/all.json`)
.then(response => response.json())
.then(json => {
let result = json.find(item => item.name === `${heroName}`);
// console.log(result)
let final = (result.id);
console.log(final)
})
return final
}
This is resulting undefined. I even tried to the final variable global and then do it but still got undefined

(JavaScript) get variable from outside of an async function [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 9 months ago.
In this piece of code, I'd like to alert the "resizedImageToUpload" variable from within the if(pickedImagePath != "") statement.
The alert within the resizeImage = async () function works fine, the one within the if(pickedImagePath != "") returns undefined.
var resizedImageToUpload;
const resizeImage = async () => {
const manipResult = await manipulateAsync(
pickedImagePath,
[
{ resize: { width: 390 }},
],
{ compress: 1, format: SaveFormat.PNG }
);
var manipResultSTR = JSON.stringify(manipResult);
var manipResultPARSED = JSON.parse(manipResultSTR);
var resizedImageToUpload = manipResultPARSED.uri;
alert("resizedImageToUpload IS: "+resizedImageToUpload);
};
if(pickedImagePath != ""){
resizeImage();
alert("resizedImageToUpload IS: "+resizedImageToUpload);
}
I guess it's because of the async function not being called yet by the time the "if(...)" is triggered.

Can not access javascript variable value [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 3 years ago.
I can not access the value of msgdata outside getJSON().
function editPost(event) {
event.preventDefault();
var msgdata = 'Hi';
$.getJSON('/users', function (data) {
msgdata = prompt("Enter value");
});
var newMessage = { 'message': msgdata }
}
Any suggestions ?
Because, the GET request completed after newMessage assignment.

Return value from callback is null. How can I structure my code to get the return value correctly? [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 3 years ago.
I have to create a number of RabbitMQ channels in succession, so I am trying to parameterize their creation in a function whose return value would be the channel. The function is declared in the callback-of-a-callback to the connection function.
var otaChannel = null;
amqp.connect(amqp_url, (err0, conn) => {
if(err0) {
throw(err0);
}
function getConsumerChannel(channelname) {
conn.createChannel((err1, ch) => {
if(err1) {
throw(err1);
}
ch.assertQueue(channelname, '', (err, q) => {
console.log(' [*] Created ' + q.queue + '.');
return ch;
});
});
}
otaChannel = getConsumerChannel(otaQName);
});
If I do "console.log(ch)" in the assertQueue function, I see the details of the channel. However, otaChannel in the final line of the code is always null. I've tried a bunch of variations of the code I posted, but cannot get the channel assigned to otaChannel. Where am I going wrong?

Pass reference variable outside the function [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 3 years ago.
In a function to obtain certain data with a json, I want to use this data in a variable and then use it in another function. The problem is that it does not work for me
First of all, obtaining the data with a json and printing it in place on the screen, until now, the problem starts when I save the data in a variable
function cargarPropuestas() {
var peticionLoc = new XMLHttpRequest();
peticionLoc.open('GET', 'https://api.ipdata.co/es?api-key=b3ae31a7a74386088875d7d285250b58778e13de985b2f561910b6bd');
peticionLoc.onload = function () {
var datosLoc = JSON.parse(peticionLoc.responseText);
var elementoLoc = document.createElement('div');
elementoLoc.innerHTML += (datosLoc.city + ', ' + datosLoc.country_name);
loc.appendChild(elementoLoc);
var country = datosLoc.country_name;
}
peticionLoc.send();
What interests me here is the variable COUNTRY
And here the problem begins, since when I "leave" the requestLoc.onload = function () {} I can not call the variable COUNTRY to use it below
What I need is to know how to get the information of this variable out of the function, in order to use that data
You can declare country outside both functions.
var country;
function cargarPropuestas() {
//...
peticionLoc.onload = function () {
//...
country = datosLoc.country_name;
}
peticionLoc.send();
}

Categories