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.
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 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 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();
});
}),
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?
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 years ago.
I know this has been asked a million times, but all I see is people explaining scope and no one explains a solution.
In this case HOW do I get alert to show the value of comment_count rather than undefined.
var comment_count;
$.post( "<?=site_url('comments/ajax_get_comment_count');?>",{tags:[aData[7],'BWQ']}, function( data ) {
comment_count = data;
});
alert(comment_count);
I think you need something like that:
var comment_count;
function controller(){
return {
setValue:function(_val,data){
_val = data;
this.fireValue(_val)
},
fireValue:function(_val){
alert(_val)
}
}
}
$.post("<?=site_url('comments/ajax_get_comment_count');?>", {tags: [aData[7], 'BWQ']}, function (data) {
controller.setValue(comment_count,data)
});