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
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 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]);
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 5 years ago.
I am writing a web scraping application. I can write this as a single script, and it will return a the array as desired. When modularizing my code, and turning it into functions, the code that should return an array returns undefined. Any pointers would be great.
"use strict";
let request = require("request");
let cheerio = require("cheerio");
function isNotUndefined(value){
if(value !==undefined){
return value;
}
}
function scrapeIndexPage(url){
var arr = new Array();
request(url, function(req,error,body){
var $ = cheerio.load(body);
var view = $("div.view-content");
view = view.html();
$ = cheerio.load(view);
for(var i=1;i<60;i++){
var getClass = ".class-row-"+i+" a";
var link = $(getClass).attr("href");
arr.push(link);
}
arr = arr.filter(isNotUndefined);
});
return arr;
}
var url = 'http://www.urlhere.com';
console.log(scrapeIndexPage(url));
This question already has answers here:
How to wait until jQuery ajax request finishes in a loop?
(5 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
I can't figure how to loop through an array of urls and pass them to getJSON, to finally return the new array of datas.
var data_num=[];
$.each(arr_urls, function( index, value ) {
$.getJSON(arr_urls[index], function (data) {});
data_num.push(data);
});
alert(data_num);
var data_num=[];
$.each(arr_urls, function( index, value ) {
$.getJSON(arr_urls[index], function (data) {
data_num.push(data);
alert(data_num);
});
});
Put your pusher and alert in the callback like this.
Why can't you just do:
var jsonData = JSON.stringify(arr_urls);
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 7 years ago.
I have had a look, and see some great examples of nesting functions, but even with reading them, I cannot see why I am getting undefined when i call this function:
function readBttnDetec() {
var devid = localStorage.getItem('vBttn');
var bd = 0;
bd = ble.read(devid, 'fffffff0-00f7-4000-b000-000000000000',
'FFFFFFF2-00F7-4000-B000-000000000000',
function(t) {
var data = new Uint8Array(t)
console.log('returns: ' + data[0]); // this returns 6
return data[0];
}, function(f) {
console.log(f);
});
return bd;
}
This is the call:
//check button state
var detecs = readBttnDetec();
console.log(detecs);
if(detecs == 2) {
// fall detection disabled
$('#playfall').removeClass('km-state-active');
} else if(detecs == 6) {
// fall detection enabled
$('#playfall').addClass('km-state-active');
} else {
// error reading button
}
I am missing something simple I am sure of it, but I cannot see it.
Thanks in advance