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 years ago.
I am trying to read the enteredValue variable outside the function but the value outside the function remains undefined:
var enteredValue;
$("input").on("keydown",function search(e) {
if(e.keyCode == 13) {
alert($(this).val());
enteredValue = $(this).val().trim().toUpperCase()
console.log("Code inside: " + enteredValue); //value is read
}
});
console.log("Code outside : " + enteredValue); //value undefined
Can you please tell me what I'm doing wrong?
Related
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.
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);
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.
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?
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 4 years ago.
I am using Ember for my webapp and I have this piece of code :
canAcceptPayPal: computed('data.event.paymentCurrency', function() {
return this.get('store').queryRecord('setting', {})
.then(setting => {
console.log(setting);
console.log(setting.paypalSandboxUsername || setting.paypalLiveUsername);
return false;
});
}),
But this only returns a promise and not the boolean value. I am using it in handlebars as :
{{#if canAcceptPayPal}}
What am I missing here?
Solution
As Amandan pointed out, re-rendering inside the then call worked:
canAcceptPayPal: computed('data.event.paymentCurrency', function() {
this.get('store').queryRecord('setting', {})
.then(setting => {
this.set('canAcceptPayPal', (setting.paypalSandboxUsername || setting.paypalLiveUsername) && find(paymentCurrencies, ['code', this.get('data.event.paymentCurrency')]).paypal);
this.rerender();
});
}),