Skip to content

Commit 0d4c1ba

Browse files
committed
[main] add promise API
Resolves pkra#19
1 parent 3ab3539 commit 0d4c1ba

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/main.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,22 @@ const preprocessor = function (data, callback) {
133133

134134
exports.start = mathjax.start;
135135
exports.config = mathjax.config;
136-
exports.typeset = function (data, callback) {
136+
const cbTypeset = function (data, callback) {
137137
if (data.enrich) {
138138
preprocessor(data, function (result) {
139139
main(result, callback);
140140
})
141141
} else main(data, callback);
142142
};
143+
// main API, callback and promise compatible
144+
exports.typeset = function (data, callback) {
145+
if (callback) cbTypeset(data, callback);
146+
else return new Promise(function (resolve, reject) {
147+
cbTypeset(data, function (output, input) {
148+
if (output.errors) reject(output.errors);
149+
else resolve(output, input);
150+
});
151+
});
152+
};
143153
exports.postprocessor = postprocessor;
144154
exports.preprocessor = preprocessor;

test/base-typeset-promise.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var tape = require('tape');
2+
var mjsre = require("../lib/main.js");
3+
4+
tape('Base: typeset promise API', function (t) {
5+
t.plan(2);
6+
7+
var tex = '';
8+
mjsre.start();
9+
10+
// promise resolved
11+
mjsre.typeset({
12+
math: tex,
13+
format: "TeX",
14+
mml: true
15+
}).then((result) => t.ok(result.mml, 'Typset promise resolved on success'));
16+
17+
mjsre.typeset({
18+
math: tex,
19+
format: "MathML",
20+
mml: true
21+
}).catch((error) => t.ok(error, 'Typeset promise rejected on error'));
22+
});

0 commit comments

Comments
 (0)