iwrestledthebeartwice
iwrestledthebeartwice

Reputation: 734

UnhandledPromiseRejectionWarning when using a function inside another function in NodeJS

I'm extracting the original URL from twitter using NodeJS and I have the following functions

First I extract the tweet and save its body in a variable as follows

getTweets().then(tweets=>{
    let tweet = tweets[0].text;
    let tcoURL = extractURL(tweet); //this function extracts the t.co url from tweet using regex
    console.log(tcoURL)
    
    //but when I execute this following function, I'm getting unhandled promise error
    extractTransactionURL(tcoURL).then(url=>{
        console.log(url)
    })
})

The extractTransactionURL function is as follows, it takes in t.co url and returns the original url

const request = require('request')

function extractTransactionURL(url){

    let headers = {
        "User-Agent": "Please gimme back the original url from the tweet"
    }
  
    var options = {
      url, headers
    }
  
    return new Promise((resolve, reject)=>{
        setTimeout(()=>{
            request.get(options, function (error, resp, body) {
                    if (!error && resp.statusCode==200) {
                        resolve(resp.request.uri.href);
                    }
                    else{
                        reject(error)
                    }
                }
            ), 250})
    }); 
}

Please help, any help would be appreciated.

Here is the full error

(node:4216) UnhandledPromiseRejectionWarning: null
(node:4216) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:4216) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Upvotes: 0

Views: 85

Answers (1)

Kai O.
Kai O.

Reputation: 166

This is likely not happening because of nested functions, but because of .then(). An UnhandledPromiseRejectionWarning happens when the promise fails, and you don't have code to handle the rejection. Here, you have only one argument for .then() which handles fulfilled promises, but you should add a second argument that is a callback for rejected promises (what happens when the promise fails and is unfulfilled).

Basically, what is happening is that in your extractTransactionURL(url) function, we end up calling reject(error) in the promise it returns. However, the .then() has no callback to handle the rejection and throws the warning.

Here is what you could do:

getTweets().then(tweets=>{
    let tweet = tweets[0].text;
    let tcoURL = extractURL(tweet); //this function extracts the t.co url from tweet using regex
    console.log(tcoURL)
    
    extractTransactionURL(tcoURL).then(url=>{
        console.log(url)
    }, err=>{
        // DO SOMETHING TO HANDLE UNFULFILLED PROMISE
    })
}, err=>{
    // DO SOMETHING TO HANDLE UNFULFILLED PROMISE
});

Upvotes: 1

Related Questions