Skip to content

Commit

Permalink
Promise API.
Browse files Browse the repository at this point in the history
  • Loading branch information
BYVoid committed Feb 6, 2017
1 parent 8385259 commit 6d47ff9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions node/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ console.log(converted);
opencc.convert("汉字", (err, converted) => {
console.log(err, converted);
});

// Async API with Promise
opencc.convertPromise("汉字").then(converted => {
console.log(converted);
});
19 changes: 19 additions & 0 deletions node/opencc.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,22 @@ OpenCC.prototype.convert = function (input, callback) {
OpenCC.prototype.convertSync = function (input) {
return this.handler.convertSync(input.toString());
};

/**
* Converts input text asynchronously and returns a Promise.
*
* @fn Promise convertPromise(string input)
* @memberof OpenCC
* @param input Input text.
* @return The Promise that will yield the converted text.
* @ingroup node_api
*/
OpenCC.prototype.convertPromise = function (input) {
const self = this;
return new Promise(function(resolve, reject) {
self.handler.convert(input.toString(), function(err, text) {
if (err) reject(err);
else resolve(text);
});
});
};

0 comments on commit 6d47ff9

Please # to comment.