This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
I can access the array 'words' in global scope but when I try to find an item by typing 'words[index]' , it returns undefined. How can I solve this?
let words = [];
function fetchWords() {
fetch("https://www.themealdb.com/api/json/v1/1/categories.php")
.then((res) => res.json())
.then((data) => {
for (let i = 0; i < 13; i++) {
words.push(data.categories[i].strCategory);
}
});
}
fetchWords();`
console.log(words); //This works
console.log(words[2]); // But this does not. Why?
This is because you call:
console.log(words[2]);
before you fetch the result. You need to await fetchWords before console.log(words[2]);
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 last month.
The community reviewed whether to reopen this question last month and left it closed:
Original close reason(s) were not resolved
Unable to return value inside function in node.js . Below is my function which I am calling from another function to get desired value but I am getting undefined.
const getBBBMeetingInfo = (meetingID) => {
let bbbHttp = bbb.http;
let getMeetingInfo = api.monitoring.getMeetingInfo(meetingID);
let val;
bbbHttp(getMeetingInfo).then((result) => {
let newRes = {};
newRes = result;
newRes.meetingID = meetingID;
if(newRes.returncode == 'FAILED'){
updateNotificationCallStatus(meetingID)
}
val = newRes.returncode
});
return val
}
calling fuction like this
let bn = getBBBMeetingInfo(ele.meetingId);
console.log(bn)//undefined
I am new to node.js. Please let me know how I can get it and use it as returned value.
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 4 years ago.
Hello I have a function that goes to the database and returns array of objects like so:
function findAlbumImages(){
remote.findAlbum.then(
res =>{
})
}
but I want to call this from another function and assign that res to array collection like so:
let newArray = findAlbumImages();
Is there a way to do this?
Sure you can, by using async/await, which is the closest you can
get to your desired syntax:
function findAlbumImages() {
return remote.findAlbum()
}
(async () => {
let newArray = await findAlbumImages()
console.log(newArray)
})()
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 4 years ago.
The below code is not returning a value back but it console logs out fine. Any ideas how I can set the value of X?
var dbSize = dbo.collection('Items').count()
var x = 0
x = dbSize.then(len => {
return len
})
This is what is being logged 'Promise { }' however if I simply write this:
dbo.collection('Items').count()
var x = 0
dbSize.then(len => {
console.log(len)
})
then It logs out fine.
you can warp your code with an async function and then use await to make your code work like synchronized code
const a = async () =>{
var dbSize = await dbo.collection('Items').count()
console.log(dbSize)
}
a();
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)
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
Closed 5 years ago.
This code returns an array of strings:
var getCitiesList = function (url, callback) {
var citiesList = [];
var search = function (needSearch,whereSearch) {
var re = new RegExp(needSearch,'ig'),
matched = whereSearch.match(re); //возвращает массив совпадений
return matched !== null;
};
$.getJSON(url)
.done(function (data) {
$.each(data, function (index, value) {
if (search(input.val(), value.City)) {
citiesList.push(value.City);
}
});
});
return citiesList;
};
When I call it here, it is looks like empty array, but contains elements, array.length === 0
input.keyup(function () {
var cities = getCitiesList('../kladr.json');
console.log(cities); //[] but contains elements
console.log(cities.length);//0
});
How it looks in browser
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
var appCounts = 0;
applicationsService.getApplicationCountByJob(job.id).then((appcount) => {
appCounts = appCounts + appcount;
});
company.applications = appCounts;
appCount has lost value in company.applications = appCounts;
Please let me know how to solve that issue.
your function getApplicationCountByJob is probably asynchronous, meaning
company.applications = appCounts;
gets executed before
appCounts = appCounts + appcount;
move inside another then to get it work:
var appCounts = 0;
applicationsService.getApplicationCountByJob(job.id)
.then((appcount) => {
appCounts = appCounts + appcount;
})
.then(() => {
company.applications = appCounts;
});