Map with async not saving element mutation [duplicate] - javascript

This question already has answers here:
When should I use a return statement in ES6 arrow functions
(6 answers)
How to return many Promises and wait for them all before doing other stuff
(6 answers)
Closed 2 years ago.
Why protocols.usd is always returning 0?
I try making a dummy promise function to test it and it's working properly but it's not working with the await function.
async function addPrices() {
const balances = await getBalances();
await Promise.all(balances.map(protocols => {
protocols.usd = 0;
protocols.balances.map(balance => {
if (balance.underlying) {
balance.underlying.map(async token => {
let price = await getPrice(token.address);
protocols.usd += price.market_data.current_price.usd;
});
}
});
}));
return balances;
}

Related

Promise { <pending> } [duplicate]

This question already has answers here:
async/await implicitly returns promise?
(5 answers)
Why is my asynchronous function returning Promise { <pending> } instead of a value?
(9 answers)
Async function returning promise, instead of value
(3 answers)
How can I access the value of a promise?
(14 answers)
What are asynchronous functions in JavaScript? What is "async" and "await" in JavaScript?
(2 answers)
Closed 4 months ago.
static async updatePaidLeave() {
const result = await PaidUnpaidLeaveSchema.find({});
console.log(result, ";;;;;;;");
}
static runCronJob() {
cron.schedule("*/10 * * * * *", async () => {
this.updatePaidLeave();
console.log(this.updatePaidLeave(), "Im running in every 10 seconds.");
});
}
Output // Promise { } Im running in every 10 seconds.
i have tried above code and i want data from PaidUnpaidLeaveSchema
The code requires a couple of fixes. Try this (look at comments):
static async updatePaidLeave() {
const result = await PaidUnpaidLeaveSchema.find({});
console.log(result, ";;;;;;;");
return result // return result
}
static runCronJob() {
cron.schedule("*/10 * * * * *", async () => {
const result = await this.updatePaidLeave(); // await and save result in a variable
console.log(result, "Im running in every 10 seconds."); // log result
});
}

returning first value [object Promise] [duplicate]

This question already has answers here:
async/await implicitly returns promise?
(5 answers)
Why is my asynchronous function returning Promise { <pending> } instead of a value?
(9 answers)
Async function returning promise, instead of value
(3 answers)
How can I access the value of a promise?
(14 answers)
What are asynchronous functions in JavaScript? What is "async" and "await" in JavaScript?
(2 answers)
Closed 8 months ago.
const wordEl = document.getElementById('word');
let selectedWord = [];
let correctLetters = [];
let apiUrl = 'https://random-word-api.herokuapp.com/word';
async function getData(url) {
let response = await fetch(url);
let data = await response.json();
selectedWord = await data;
}
selectedWord = getData(apiUrl);
async function displayWord() {
wordEl.innerHTML = `
${selectedWord
.toString()
.split('')
.map(
(letter) =>
`
<span class="letter">
${correctLetters.includes(letter) ? letter : ''}
</span>
`
)
.join('')}
`;
}
window.addEventListener('keydown', (e) => {
if (selectedWord.toString().includes(e.key)) {
if (!correctLetters.includes(e.key)) {
correctLetters.push(e.key);
displayWord();
}
}
});
displayWord();
Hello my first value returning [object promise]. After pressing any key value returning what I want.
How can i get normal value before clicking any key? Thank you!

How to await a forEach method [duplicate]

This question already has answers here:
Wait until all jQuery Ajax requests are done?
(22 answers)
Why is my function returning Promise { <pending> } [duplicate]
(1 answer)
Closed 2 years ago.
How do I await the forEach function so that I can console.log(posters)?
let posters = [];
reviews.forEach(async (review) => {
// Get posters for reviews
await axios.get(`https://api.themoviedb.org/3/movie/${review.filmId}?api_key=${tmdb_api}&language=en-US`)
.then((data) => {
posters.push(data.data.poster_path)
})
.catch((error) => console.log(error))
})
console.log(posters)
Important: I cannot put the console.log inside the .then() because I need the filled posters array outside of the forEach function.

How to use an array which is returned from 1 function in Nodejs? [duplicate]

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]);
}

What is the difference b/w the two async/await functions? [duplicate]

This question already has answers here:
Difference between `return await promise` and `return promise`
(7 answers)
Closed 5 years ago.
const displaySymbols = async (symbols) => {
const sym = await Promise.all(symbols.map(s => {
// createEl return a promise
return createEl(s)
}))
return sym
}
const displaySymbols = async (symbols) => {
const sym = await Promise.all(symbols.map(async s => {
return await createEl(s)
}))
return sym
}
The results are same, without Promise.all, sym would be always a promise array, no matter createEl is await-ed or not, then is it necessary to use async function as map method?
P.S. The code is just a demo.
The second one is superflous. Its like:
Promise.resolve( new Promise() )

Categories