0

I'm working on nodejs. Currently I'm doing the recursive part of my work.

In prodHelper.js

product: (data, totalUser) => {
    return new Promise(async (resolve, reject) => {
        // data = {total: 5, order: 6, status: active} -> As a dynamic data
        let number = 1
        let res = await prodHelper.groupProduct(data, totalUser, number)
        console.log('RESULT', res) // undefined
    })
}
groupProduct: (data, totalUser, number) => {
    return new Promise(async (resolve, reject) => {
        let result
        if (data.total < totalUser) {
            result = true
        } else {
            let nextNumber = number;
            if (number < totalUser) {
                nextNumber = nextNumber + 1
            } else if (number == totalUser) {
                nextNumber = number - totalUser
            }
            if (nextNumber >= 0 && nextNumber < totalUser) {
                await prodHelper.groupProduct(data, totalUser, nextNumber)
            } 
        }
        return resolve(result)
    })
}

I have run my code recursively successfully. But my problem here is when I call product function it waits for groupProduct function to run and return the result. But the groupProduct function returns the results of the first run, not waiting for the results of the recursive run.

It will return undefined, because it does not wait for recursion. Is there a way for it to wait for the recursion to finish and then return resolve(result).

I tried that. But it returns HTTP status code 503 :

groupProduct: (data, totalUser, number) => {
    return new Promise(async (resolve, reject) => {
        let result
        if (data.total < totalUser) {
            result = true
        } else {
            let nextNumber = number;
            if (number < totalUser) {
                nextNumber = nextNumber + 1
            } else if (number == totalUser) {
                nextNumber = number - totalUser
            }
            if (nextNumber >= 0 && nextNumber < totalUser) {
                await prodHelper.groupProduct(data, totalUser, nextNumber)
            } 
        }
        if (result) {
            return resolve(result) //error 503 : Service Unavailable
        }
    })
}

I am new to nodejs language so this is giving me a hard time. Can anyone give me any ideas. Thanks.

Miedkes
  • 589
  • 1
  • 5
  • 16
  • Can you please how are you calling `product` function, with what parameters basically? – Charchit Kapoor Mar 20 '23 at 04:11
  • 1
    [Never pass an `async function` as the executor to `new Promise`](https://stackoverflow.com/q/43036229/1048572)! – Bergi Mar 20 '23 at 04:12
  • 2
    "*returns the results of the first run, not waiting for the results of the recursive run.*" - oh it does wait, you're just not doing anything with the `await`ed return value of the recursive call. After the line of the recursive call, `result` simply still is `undefined`. – Bergi Mar 20 '23 at 04:14
  • Not really relevant to your question, but is there a reason not represented in your sample code that this needs to use promises/be async? – ray Mar 20 '23 at 04:17
  • @CharchitKapoor call `product` function i use api. I knew someone would ask, so I wrote an example `// data = {total: 5, order: 6, status: active} -> As a dynamic data` – Miedkes Mar 20 '23 at 04:23
  • @Bergi Actually, I've just switched to nodejs from php. I used `promise` to wait. And inside the if `data.total < totalUser` function I also tried returning `result resolve(true) ` but still no okay. I'm really stuck – Miedkes Mar 20 '23 at 04:27
  • 1
    There's nothing asynchronous here so there's no reason to use promises at all. – jfriend00 Mar 20 '23 at 04:28
  • 1
    `await prodHelper.groupProduct(data, totalUser, nextNumber)` should be this `result = await prodHelper.groupProduct(data, totalUser, nextNumber)` – Charchit Kapoor Mar 20 '23 at 04:29
  • @CharchitKapoor Great answer. Please create a separate answer. I will vote for your answer – Miedkes Mar 20 '23 at 04:44
  • @Miedkes Recursion doesn't really work different than in PHP. Making the recursive call and awaiting it are one thing, returning its result is another. – Bergi Mar 20 '23 at 09:59

1 Answers1

1

As pointed out in the comments by @Bergi, in the groupProduct function, the statement:

await prodHelper.groupProduct(data, totalUser, nextNumber)

should be

result = await prodHelper.groupProduct(data, totalUser, nextNumber)
Charchit Kapoor
  • 8,934
  • 2
  • 8
  • 24