0

This is a simplified version of my code

1| client.on("message", async message => {
2|    if( ... ){
3|        let object = null
4|        if(object === null) object = await getObj() // <- Returns a promise
5|
6|        let variable = object.property 

My problem is that since line 3 is null, line 4 gets executed, and getObj is a async local function. But for some reason the code does not wait for line 4 to return a promise but instead keeps on running causing line 6's object to be null. Meaning I am receiving a "Cannot read property id of null" error

Here is a simplified version of getObj

    async function getObj(){
        let clt = a event

            clt.on("collect", msg => {
                return a object
            })

            clt.on("end", clted => {
                if(clted.size === 0){
                    return null
                }
            })
    }

Here is the raw version of my code: https://sourceb.in/PdGSJCf3x1

Onii-Chan
  • 57
  • 1
  • 6
  • Simplified version of `getObj` function returns a promise that is fulfilled with the value of undefined. Returning a value from the callback function does not make it a return value of the outer function. – Yousaf Jun 16 '21 at 10:29
  • Btw the function does get executed – Onii-Chan Jun 16 '21 at 10:29
  • Yousaf, I did think of that so i added a return "hello" at the bottom of my getObj function but its still the same – Onii-Chan Jun 16 '21 at 10:30
  • `getObj` function is executed until the end and the promise it returns is resolved immediately because there is not `await` inside it - it does not _wait_ for any async operation to complete. Try returning this object: `{ property: "hello" }` from this function. – Yousaf Jun 16 '21 at 10:32

0 Answers0