0
votes

I want to make a whatapp bot brainly nodejs. And I want to add an argument. my code is like this:

                case 'brainly':
                const args = process.argv
                    brainly(args).then(res => {
                        for(var i=0; i<res.length; i++){
                        var jawab = res[i].jawaban
                        client.sendText(from, jawab[0].text)
                        }
                    })
                break

But, when I run it shows an error like this:

(node:7960) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().

how to fix this? Please help me.

1
Have you tried adding a .catch() like the error message suggests?Phil

1 Answers

2
votes

This means that somewhere in an asynchronous context, most likely inside of the promise created by calling brainly(args), an error occurred, and it wasn't caught and dealt with.

To get started with asynchronous Javascript, I recommend finding some articles about Javascript Promises and Async Functions (that implicitly generate promises, and allow for the await syntax).

There's no real easy way to explain. Javascript is a single-threaded language, but it offers great concurrency through the use of callbacks, promises and the async/await syntax (they all sort of originate from callbacks). Anything that does not happen instantaneously, such as network/disk operations, is asynchronous and must be handled in an appropriate manner. This can be confusing for newcomers as it breaks up the linear asyncronous flow of execution.

Promises are objects that can be created that will either resolve or reject in the future. By calling .then on the promise, you are giving it a callback function that will be called with the resolved value, when it eventually arrives. You should also add a .catch(err => { ... }) handler to the end to deal with errors, which is probably being thrown for some reason as a result of your passed arguments.