diff --git a/docs/USING_PRO.md b/docs/USING_PRO.md index 12a60e172a..a665122641 100644 --- a/docs/USING_PRO.md +++ b/docs/USING_PRO.md @@ -255,7 +255,7 @@ smartypants('"this ... string"')
walkTokens
An error occurred:
' + const msg = 'An error occurred:
' + escape(e.message + '', true) + ''; + if (opt.async) { + return Promise.resolve(msg); + } + return msg; + } + if (opt.async) { + return Promise.reject(e); } throw e; } try { + if (opt.async) { + let promise = Promise.resolve(Lexer.lex(src, opt)); + if (opt.walkTokens) { + promise = promise.then((tokens) => + Promise.all(marked.walkTokens(tokens, opt.walkTokens)).then(() => tokens) + ); + } + return promise.then((tokens) => Parser.parse(tokens, opt)).catch(onError); + } + const tokens = Lexer.lex(src, opt); if (opt.walkTokens) { - if (opt.async) { - return Promise.all(marked.walkTokens(tokens, opt.walkTokens)) - .then(() => { - return Parser.parse(tokens, opt); - }) - .catch(onError); - } marked.walkTokens(tokens, opt.walkTokens); } return Parser.parse(tokens, opt); } catch (e) { - onError(e); + return onError(e); } } diff --git a/test/unit/marked-spec.js b/test/unit/marked-spec.js index e9bfba1782..4dab6aeaac 100644 --- a/test/unit/marked-spec.js +++ b/test/unit/marked-spec.js @@ -1112,4 +1112,14 @@ br const html = await promise; expect(html.trim()).toBe('text walked
'); }); + + it('should return promise if async', async() => { + marked.use({ + async: true + }); + const promise = marked('*text*'); + expect(promise).toBeInstanceOf(Promise); + const html = await promise; + expect(html.trim()).toBe('text
'); + }); });