Skip to content

Commit 6d47ff9

Browse files
committed
Promise API.
1 parent 8385259 commit 6d47ff9

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

node/demo.js

+5
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,8 @@ console.log(converted);
4141
opencc.convert("汉字", (err, converted) => {
4242
console.log(err, converted);
4343
});
44+
45+
// Async API with Promise
46+
opencc.convertPromise("汉字").then(converted => {
47+
console.log(converted);
48+
});

node/opencc.js

+19
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,22 @@ OpenCC.prototype.convert = function (input, callback) {
106106
OpenCC.prototype.convertSync = function (input) {
107107
return this.handler.convertSync(input.toString());
108108
};
109+
110+
/**
111+
* Converts input text asynchronously and returns a Promise.
112+
*
113+
* @fn Promise convertPromise(string input)
114+
* @memberof OpenCC
115+
* @param input Input text.
116+
* @return The Promise that will yield the converted text.
117+
* @ingroup node_api
118+
*/
119+
OpenCC.prototype.convertPromise = function (input) {
120+
const self = this;
121+
return new Promise(function(resolve, reject) {
122+
self.handler.convert(input.toString(), function(err, text) {
123+
if (err) reject(err);
124+
else resolve(text);
125+
});
126+
});
127+
};

0 commit comments

Comments
 (0)