I have created a multiple promises array using meteor.call and map functions. and triggered the promises using Promise.all function but i am getting the same result as each promises result. Here is my code:
//react function
sync = async () => {
try {
const data = await new Promise(async (resolve) => {
let limit = 2;
let skip = 0;
skip = skip - limit;
const count = await new Promise(async (resolve) => Meteor.call("fetchData", 50000, 0, true, (error, result) => resolve(result)));
let i = [1]
if (count == 0) resolve([])
if (count > limit) i = Array.from(Array(Math.round(count / limit)).keys())
const resultPromises = i.map(async j => {
return await new Promise(async (resolve) => await Meteor.call("fetchData", limit, skip = skip + limit, false, (error, result) => resolve(result)));
})
const resultArray = await Promise.all(resultPromises);
console.log(resultArray)
resolve(resultArray);
}
);
}catch(e){console.error(e)}
}
Thanks for the reply, I have tried forEach but not fixed the issue its getting the same result,
Also in for loop i couldn’t call the async functions(Meteor.call) inside loop.
Not working
Sorry here is the full code, its all happens inside a promise function. tried all type looping functions but the meteor call results are getting as combined value
//react function
sync = async () => {
try {
const data = await new Promise(async (resolve) => {
let limit = 2;
let skip = 0;
skip = skip - limit;
const count = await new Promise(async (resolve) => Meteor.call("fetchData", 50000, 0, true, (error, result) => resolve(result)));
let i = [1]
if (count == 0) resolve([])
if (count > limit) i = Array.from(Array(Math.round(count / limit)).keys())
const resultPromises = i.map(async j => {
return await new Promise(async (resolve) => await Meteor.call("fetchData", limit, skip = skip + limit, false, (error, result) => resolve(result)));
})
const resultArray = await Promise.all(resultPromises);
console.log(resultArray)
resolve(resultArray);
}
);
}catch(e){console.error(e)}
}
It seems like you’re returning the result of the promise inside the map rather than promise to be awaited. To get more verbosity on what’s happening could you paste some output by changing this:
to:
const resultPromises = i.map(async j => {
const res = await new Promise(async (resolve) => await Meteor.call("fetchData", limit, skip = skip + limit, false, (error, result) => resolve(result)));
console.log( {res, limit, skip} );
return res;
})
const resultArray = await Promise.all(resultPromises);
console.log({resultArray, resultPromises});//are result Promises promises or the result already?
Thanks for the response, I have tried to fetch results using the map function and index, but the each results are same (total combined result).
Total records in db is 14 and i used the limit 2, its generated 7 objects and it should have 2 records in each objects but each objects have 14 records now
CODE: