This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Async Function leaving array blank outside of scope [duplicate]
(4 answers)
Closed 9 months ago.
I have started learning javascript recently and started a new project.
So my problem here is that have this call to an API, everything is good I can access to the names of the pokemons. But when I try to get a return it keep telling me is undefined. I have already try diferents solutions but nothings works.
Console of edge
function arrayOptions(pk) {
fetch('https://pokeapi.co/api/v2/pokemon?limit=10')
.then((res) => res.json())
.then((data) => {
for (let i = 0; i < data.results.length; i++) {
console.log(i, data.results[i].name);
pk.push(data.results[i].name);
}
});
return pk;
}
var pk = [];
pk = arrayOptions(pk);
console.log(pk[0]);
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.
Iam trying to render for Cars from my API. My problem is order in witch this fucntion is called.
export default function getGarage(arrayGarage) {
fetch('http://127.0.0.1:3000/garage')
.then(res => res.json())
.then(res => {
arrayGarage = res
console.log(arrayGarage)
})
if (arrayGarage) {
console.log(arrayGarage)
return arrayGarage
}
}
let arrayGarage = []
arrayGarage = getGarage(arrayGarage)
if (arrayGarage) {
console.log('before while')
for (let i = 0; i < arrayGarage.length; i++) {
console.log('in while')
createCar(arrayGarage[i])
}
}
In first step I got an empty Array (from IF console in fetch), in second step I got "before while" and in third step I got array with my Cars(form then console). How to do that my 'while' wait for property from fetch and handle while.
[] apiFunctions.js:13
before while garage.js:43
(4) [{…}, {…}, {…}, {…}] apiFunctions.js:10
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Angular: returning a value from onValue() in firebase realtime database
(2 answers)
Closed 3 months ago.
I use NextJS
const db = getDatabase()
const getTeamData = (teamName: string) => {
onValue(ref(db, teamName), snapshot => {
return snapshot.val()
})
}
console.log(getTeamData('exampleTeam'))
I do have this exampleTeam in the database; however, it returns undefined anyway. But why?
I have tried this:
let ret: any[] = []
onValue(ref(db, teamName), snapshot => {
ret.push(snapshot.val())
})
return ret[0]
}
But it still returns undefined.
Also, if I change something in the code so that it does a fast reload, then all of the correct data is printed and it works good.
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 2 years ago.
I prepared a function that reads a json file and I want to return an array that I can then use to create charts.
The function reads the file correctly and generates the array as intended within the fetch() function, but I am not able to pass it further. What I am doing wrong?
function getOrderDays(file, col){
const myRequest = new Request(file);
fetch(myRequest)
.then(response => response.json())
.then(data => {
var selecteddata = [];
data.forEach(function(item) {
selecteddata.push(parseInt(item[col]));
})
// Create an array containing the indexes of values > 0 :
var indexes = [0]; // I need a 0 at the beginning
var i = 0;
while (data[i]) {
if (selecteddata[i] != 0) {
indexes.push(i)
}
i++;
}
console.log(indexes); // This produces the result as needed
}).catch(err => console.error(err));
return indexes; // This is returning an empty array
}
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
I can't find way to fix this bug, I tried so many things but it's not working, IDK what am I doing wrong.
I have a function which should return array of objects with questions, but it returns array with length 0 so I can't iterate over it. I am using axios to make api call.
I am also bubble sorting the difficulty property inside the array object, so it starts from easy and goes to hard.
let getQuestions = () => {
let questions = [];
axios
.get('https://opentdb.com/api.php?amount=10')
.then((res) => {
for (let i = 1; i < res.data.results.length; i++) {
for (let j = 0; j < res.data.results.length - 1; j++) {
if (
res.data.results[j].difficulty.charCodeAt(3) <
res.data.results[j + 1].difficulty.charCodeAt(3)
) {
const temp = res.data.results[j];
res.data.results[j] = res.data.results[j + 1];
res.data.results[j + 1] = temp;
}
}
}
for (const i in res.data.results) questions.push(res.data.results[i]);
})
.catch((err) => console.error(err));
return questions;
};
console.log(getQuestions());
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
If you are using the returned value then the issue is axios takes time to fetch the data from the API, but in the meantime control moves forward and executes the return statement at which point the value of questions is []. I would suggest using async await syntax instead of then() callback. You simply await for the response from the API and when the response is received the only you return the response from the function.
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 3 years ago.
I have 1 function in Nodejs that return a list of recipient:
var recipientList = [];
userService.listUser().then( result => {
var listPSID = result.filter(result => !'279'.includes(result));
recipientList = listPSID.slice();
console.log(recipientList);
});
Now I want to access that list OUTSIDE the function, something likes:
console.log(recipientList[0]);
How can I do that? Thank you all.
If you're using Promises, you cannot access it outside the function. If you use async / await instead, it should work.
async function getList(){
let result = await userService.listUser();
var listPSID = result.filter(result => !'279'.includes(result));
recipientList = listPSID.slice();
console.log(recipientList[0]);
}