DD apps
DD apps

Reputation: 57

Cheerio error handler node js

I start a new project using Node.js and I'm wondering if there is any way to manage what returns from cheerio.load() function. I've tried to use callback and promises(then and catch) but it did't work.

example:

 var $ = cheerio.load(html);
 //what if it's falied ???? how I can handle it?

I'm asking this because I tried to run script serval time but sometimes it's work and sometimes not.

BTW: I'm using cheerio module from npm site -> npm install cheerio.

thanks :-)

Upvotes: 2

Views: 3164

Answers (1)

wintermute
wintermute

Reputation: 130

You can use a try/catch block since you do not know if the cheerio operation will succeed.

const cheerio = require('cheerio')

try {
  const $ = cheerio.load(html)
} catch (e) {
  console.log(e) // handle error
}

console.log('continue script')

Upvotes: 2

Related Questions