node.js - How to promisify nodejs child_process exec to work with es6 -
i have phantomjs script takes url in command line args , return body html content in stdout.
when receive html content via promise call via phantomjs script call parsepage() manipulate data via cheerio
, gathernext().
let cmd = '/usr/local/bin/phantomjs ' + __dirname + '/phantom.js'; let child = (command) => { return exec(command) .then(result => { return result.stdout; }); }; let parsepage = (result) => { debug('stdout %s', result); var $ = cheerio.load(result); // logic - page crawl data & 've data array of objects return resultarray }; let gathernext = (products) => { debug('products %j', products); allproducts = allproducts.concat(products); if(/*not hasnextpage*/) { return allproducts; } else { return promise.resolve(cmd + ' ' + data.productlink + '&page=' + page).then(child)// this.parse({url: `&page=${page}`}) .then(parsepage) .then(gathernext); } }; debug('parsing page page'); return promise.resolve(cmd + ' ' + data.productlink + '&page=' + page).then(child).then(parsepage).then(gathernext);
but not able promisify exec function? how can achieve this?
edit:
i've found lib: github.com/sindresorhus/execa resolves promise issue issue i'm getting. every next page (i.e, page - paginating 2, 3, 4, 5, etc) child func promise return old one's (first page's html content) next pages.. why happening in case? idea?
here's modified code this:
let child = (pager) => { let command = cmdbase + pager; console.log('command: ', command); return execa.shell(command).then(result => { return result.stdout; }); };
or, better solution?
thanks!
Comments
Post a Comment