File tree 2 files changed +24
-0
lines changed
2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -41,3 +41,8 @@ console.log(converted);
41
41
opencc . convert ( "汉字" , ( err , converted ) => {
42
42
console . log ( err , converted ) ;
43
43
} ) ;
44
+
45
+ // Async API with Promise
46
+ opencc . convertPromise ( "汉字" ) . then ( converted => {
47
+ console . log ( converted ) ;
48
+ } ) ;
Original file line number Diff line number Diff line change @@ -106,3 +106,22 @@ OpenCC.prototype.convert = function (input, callback) {
106
106
OpenCC . prototype . convertSync = function ( input ) {
107
107
return this . handler . convertSync ( input . toString ( ) ) ;
108
108
} ;
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
+ } ;
You can’t perform that action at this time.
0 commit comments