[Solved] Getting same results for all Meteor.call results in loop

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

      }

Here is the result:
image

I don’t get it, you call the same method with the same params (no params) and expect different results?

sorry the parameters are different will update it in the post

Meteor.call("fetchData",  limit, skip = skip + limit, (error, result)

These are the parameters

You can’t do this inside the map function. You’ll end up calling the same params. If you need to put them in loop, use for loop.

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.

You can build the array of params first, then use .map function on that array to call the method.

const limit = 2;
let skip = 0;
const params = [];
for (let n = 0; n < i.length; n += 1) {
  params.push({ limit, skip });
  skip += limit;
}
// make sure your params array is correct.
// console.log(params)
const results = Promise.all(params.map((p) => {
  ...
}))

Thanks @minhna will try this.

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?

And let’s see what console log do we have.

Thanks for your response,
i got this result.

#1 @smilingis is right. You are awaiting the Promise, so it will wait for the results instead of returning the Promise.

#2 You can use the index of a map function i.e. array.map((element, index) => {})

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:

let resultArray = []
        i.map((element, index) => {
          resultArray[index] = new Promise((resolve) => Meteor.call("fetchData", limit, skip = skip + limit, false, (error, result) => resolve(result)));
        })

        console.log(resultArray);

RESULT:

Also tried without promise, but getting the same result.

let resultArray = []
        i.map(async (element, index) => {
          await Meteor.call("fetchData", limit, skip = skip + limit, false, (error, result) => resultArray[index] =  result)
        })

console.log(resultArray);

result:
image

You will use the index to compute the skip value.

ohh thanks @rjdavid will try it

I created a new array for skip values , but getting the same result
CODE:

let resultArray = []
        let skipArray = []
        i.map( (element, index) => {
          skip = skip + limit
          skipArray[index] = skip;
        })
        console.log(skipArray,"skipArray")
        i.map( (element, index) => {
          console.log(skipArray[index])
           Meteor.call("fetchData", limit, skipArray[index] , false, (error, result) => resultArray[index] =  result)
        })

        console.log(resultArray,'resultArray');

RESULT:
image

You keep on computing skip as skip = skip + limit

This now looks like a programming test. How do you get the skip value by using only index and limit variables?

Tried the index instead of skip but getting the same result

let resultArray = []
        i.map( (element, index) => {
           Meteor.call("fetchData", limit, index , false, (error, result) => resultArray[index] =  result)
        })

        console.log(resultArray,'resultArray');

result

image

can be done easier, assuming that second parameter is the amount of entries to be skiped:

i.map(async (element, index) => {
          await Meteor.call("fetchData", limit, index * limit, false, (error, result) => resolve(result))
        })

i assume “fetchData” method (on the server) should be investigated next as it may have errors too.

Go to that method and print out there the parameters received and the output received at the end of that method.

Provide the results along with the methods code.