Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to return final result using recursion in nodejs

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading