Skip to content

Commit

Permalink
FIX netgusto#17 : return Promise when no callback is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
zipang committed Jun 22, 2017
1 parent 96aca2b commit 6deeacf
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/upndown.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,35 @@ module.exports = class upndown {
}

convert(html, cbk, options) {
this.parse(html, function(err, dom) {
if(err) { return cbk(err, null); }
return this.convertDom(dom, function(err2, markdown) {
if(err2) { return cbk(err2, null); }
return cbk(null, markdown);
}, options);
}.bind(this));
if (typeof cbk === "function") {
// callback style
this.parse(html, function(err, dom) {
if(err) { return cbk(err, null); }
return this.convertDom(dom, function(err2, markdown) {
if(err2) { return cbk(err2, null); }
return cbk(null, markdown);
}, options);
}.bind(this));
} else {
// returns Promise
var upnd = this;
return new Promise(function(resolve, reject) {
upnd.parse(html, function(err, dom) {
if (err) {
reject(err);
} else {
upnd.convertDom(dom, function(err2, markdown) {
if (err2) {
reject(err2);
} else {
resolve(markdown);
}

}, cbk); // cbk param is in fact the provided options
}
}.bind(upnd));
});
}
}

convertDom(dom, cbk, { keepHtml = false } = {}) {
Expand Down

0 comments on commit 6deeacf

Please sign in to comment.