How to convert concurrent request to Promise.all? [duplicate] - javascript

This question already has answers here:
Using async/await with a forEach loop
(33 answers)
How to handle multiple async requests?
(2 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 3 days ago.
Help with a little problem, please. There are two simultaneous requests, you need to redo them with Promise.all / allSettled
The function itself looks like this
async function getUsers(): Promise<void> {
usersState.forEach(async (user) => {
const { response } = await makeRequest(
userApi.getUsersById({
userId: user.id,
limit: 99
...
}),
responseStatus
)
if (response !== undefined) {
user.isActiveUser = response.length > 0
}
})
...some action
}
I understand how to use two queries that are independent of each other, but here one is inside the other...

Related

This function keeps returning undefined [duplicate]

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

Map with async not saving element mutation [duplicate]

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

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

sqlite3 db asynchronous problem in function [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 4 years ago.
I have a problem with function check_mod.
function check_mod(user_id) {
db.each(`SELECT mode FROM users WHERE id = ` + user_id, [], (err, row) => {
if (err) {
throw err;
}
if(row.mode == 1)
{
return true;
}
});
}
if(check_mod('286927644405137408')) console.log("okay");
When i try to use this function, it returns "undefined" and i don't know why. I think that i should use async and await but i don't know how to use it.
I'm not sure but, I do notice that there is no semicolon at the end of the sql statement which could be causing the issue.

Extract internal variable from anonymous function [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
as the title says I want to extract the variable status to be able to use it in another part of the code, is this possible?
RNAudioStreamer.status((err, status)=>{if(!err) console.log(status)})
Depending on the callback sync/async you can create a global variable or use promise.
let p = new Promise((resolve, reject) => {
RNAudioStreamer.status((err, status)=>{
if(!err) resolve(status); else reject(err);
})
});
// ...
p.then(status => {
// process status here
})

Categories