Javascript - Variable loose it's value after block [duplicate] - javascript

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
Can somebody explain me how to solve this problem?
let konyvSzam=0;
db.checkPeldanySzam(rentISBN,(konyv) => {
konyvSzam=konyv.Peldanyszam;
});
console.log("Nr of books");
console.log(konyvSzam);
The variable gets the value but after I check it doesn't work

Mostly our DB call works asynchrous in node js and JavaScript try to enclose db calls in promise or use async/await .

Related

How to use biri in vanilla js? [duplicate]

This question already has answers here:
Async function returning promise, instead of value
(3 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 9 months ago.
I am trying to use biri on my browser (vanilla javascript) I am following the tutorial but I have an error saying I can't use the await statement.
I've tried to create a async function to return the uniqueId but it return a promise object. I am struggle since more than a hour to get this uniqueId.
Any suggestion ?

Different logs for the same global variable (Node.js) [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)
Using async/await with a forEach loop
(33 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 11 months ago.
I have this code in my Node.js application.
The log inside forEach gives desired result but the one outside forEach gives an empty array.
let biddedListings = [];
bids.forEach(async (bid) => {
const listing = await ListingModel.find({ _id: bid.bidOn });
biddedListings.push(listing[0]);
// console.log("bidded", biddedListings);
});
console.log("bidded", biddedListings);
Whys is it that the same global variable is giving different results? What am I doing wrong here?

How to store values in global variables in async iterators? [duplicate]

This question already has answers here:
What are the rules for JavaScript's automatic semicolon insertion (ASI)?
(7 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
I started learning async iterators and could not figure how to store the results in global variables to use them later on.
let arr = [];
(async () => {
for await (let item of items) {
console.log(item);
// arr.push(item) HOW DO I PUSH THAT INSIDE?
}
})()
I declared Symbol.asyncIterator method on the object. Please check codepen https://codepen.io/INR4GE/pen/XWbVxoy?editors=1111 . Dunno why it errors on line 13, in browser it works fine. Please don't ask why am i even doing it this way. It is just for learning purposes

JQuery does not shows data in other function with the same command [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 years ago.
newbie here. I have a weird issue here where I two functions in my script. Here is the first function:
$(document).on("click", "#result2 p", function(){
var inv_id = $(this).text();
$.get("autocomplete_inv.php", {term: inv_id}).done(function(data){
var inv_info = jQuery.parseJSON(data);
$('#prod_id_'+id).val(inv_info[0]);
$('#prod_type_'+id).val(inv_info[1]);
$('#prod_colour_'+id).val(inv_info[2]);
$('#prod_price_'+id).val(inv_info[3]);
console.log('Have Data Here:',$('#prod_price_1').val());
});
$('#result2').empty();
sumPrice();
});
And here is the second function:
function sumPrice(){
console.log('No Data here:',$('#prod_price_1').val());}
The problem is, the output console.log('Have Data Here:',$('#prod_price_1').val()); only shows is the first function and not the second function. I still can't figure out what's the issue here!
Here is the screenshot:
Screenshot
Can anyone helps me on this matter? I've been stuck at this part for almost a week. I would sincerely appreciate your answer(s) very much!
Its because ajax request is already running when the control reaches sumprice function. You should write your function inside ajax.done itself.

jQuery variables turning out "undefined" even though made global [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
I want to store a value from a JSON object in a global variable to use later in my code. Apparently this does not work even though it should make the country variable global.
$.getJSON(reverseGeoRequestURL, function(reverseGeoResult){
window.country = reverseGeoResult.results[0].locations[0].adminArea1;
});
console.log(window.country);
The getJSON call is asynchronous. Put that console.log call inside the callback and it will not be undefined.

Categories