Skip to content

Commit 042dcc5

Browse files
UziTechstyfle
andauthored
fix: always return promise if async (#2728)
* fix: always return promise if async * docs: await walktokens when async * docs: walktokens async Co-authored-by: Steven <[email protected]> * return rejected promise on error --------- Co-authored-by: Steven <[email protected]>
1 parent 3acbb7f commit 042dcc5

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

docs/USING_PRO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ smartypants('"this ... string"')
262262

263263
<h2 id="walk-tokens">Walk Tokens : <code>walkTokens</code></h2>
264264

265-
The walkTokens function gets called with every token. Child tokens are called before moving on to sibling tokens. Each token is passed by reference so updates are persisted when passed to the parser. The return value of the function is ignored.
265+
The walkTokens function gets called with every token. Child tokens are called before moving on to sibling tokens. Each token is passed by reference so updates are persisted when passed to the parser. When [`async`](#async) mode is enabled, the return value is awaited. Otherwise the return value is ignored.
266266

267267
`marked.use()` can be called multiple times with different `walkTokens` functions. Each function will be called in order, starting with the function that was assigned *last*.
268268

src/marked.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,28 +108,38 @@ export function marked(src, opt, callback) {
108108
function onError(e) {
109109
e.message += '\nPlease report this to https://github.com/markedjs/marked.';
110110
if (opt.silent) {
111-
return '<p>An error occurred:</p><pre>'
111+
const msg = '<p>An error occurred:</p><pre>'
112112
+ escape(e.message + '', true)
113113
+ '</pre>';
114+
if (opt.async) {
115+
return Promise.resolve(msg);
116+
}
117+
return msg;
118+
}
119+
if (opt.async) {
120+
return Promise.reject(e);
114121
}
115122
throw e;
116123
}
117124

118125
try {
126+
if (opt.async) {
127+
let promise = Promise.resolve(Lexer.lex(src, opt));
128+
if (opt.walkTokens) {
129+
promise = promise.then((tokens) =>
130+
Promise.all(marked.walkTokens(tokens, opt.walkTokens)).then(() => tokens)
131+
);
132+
}
133+
return promise.then((tokens) => Parser.parse(tokens, opt)).catch(onError);
134+
}
135+
119136
const tokens = Lexer.lex(src, opt);
120137
if (opt.walkTokens) {
121-
if (opt.async) {
122-
return Promise.all(marked.walkTokens(tokens, opt.walkTokens))
123-
.then(() => {
124-
return Parser.parse(tokens, opt);
125-
})
126-
.catch(onError);
127-
}
128138
marked.walkTokens(tokens, opt.walkTokens);
129139
}
130140
return Parser.parse(tokens, opt);
131141
} catch (e) {
132-
onError(e);
142+
return onError(e);
133143
}
134144
}
135145

test/unit/marked-spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,4 +1112,14 @@ br
11121112
const html = await promise;
11131113
expect(html.trim()).toBe('<p><em>text walked</em></p>');
11141114
});
1115+
1116+
it('should return promise if async', async() => {
1117+
marked.use({
1118+
async: true
1119+
});
1120+
const promise = marked('*text*');
1121+
expect(promise).toBeInstanceOf(Promise);
1122+
const html = await promise;
1123+
expect(html.trim()).toBe('<p><em>text</em></p>');
1124+
});
11151125
});

0 commit comments

Comments
 (0)