-
Notifications
You must be signed in to change notification settings - Fork 3.4k
fix: always return promise if async #2728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,28 +108,35 @@ export function marked(src, opt, callback) { | |
function onError(e) { | ||
e.message += '\nPlease report this to https://github.com/markedjs/marked.'; | ||
if (opt.silent) { | ||
return '<p>An error occurred:</p><pre>' | ||
const msg = '<p>An error occurred:</p><pre>' | ||
+ escape(e.message + '', true) | ||
+ '</pre>'; | ||
if (opt.async) { | ||
return Promise.reject(msg); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It’s unusual to reject with a string. Usually it’s an Error There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is in silent mode so maybe we should actually resolve with the string? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently the string is returned in silent mode instead of thrown There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated it to resolve on silent and reject with error when not in silent mode. This seems to be the closest async equivalent to what happens when synchronous. |
||
return msg; | ||
} | ||
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); | ||
} | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.