Skip to content

Commit

Permalink
fix: Preprocessors may return promises, but not content.
Browse files Browse the repository at this point in the history
The original API for Preprocessors assumed callback.
Now promises are a valid return.
If an implementer returns a promise from `process`
but only resolves content via the callback,
then while the result of `process` is a promise,
it will not resolve to the intended content.
An easy example is for `process` to be an `async` function.
e.g.
```
async function process(content, file, done) {
   const newContent = await magic(content)
   done(newContent)
}
```
  • Loading branch information
seebees committed Oct 17, 2019
1 parent 1cd87ad commit 4620550
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion lib/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,34 @@ function executeProcessor (process, file, content) {
}
}
})
return process(content, file, done) || donePromise

/* The original API for Preprocessors assumed callback.
* Now promises are a valid return.
* If an implementer returns a promise from `process`
* but only resolves content via the callback,
* then while the result of `process` is a promise,
* it will not resolve to the intended content.
* An easy example is for `process` to be an `async` function.
* e.g.
* ```
* async function process(content, file, done) {
* const newContent = await magic(content)
* done(newContent)
* }
* ```
*/
const result = process(content, file, done)
return result && result.then
? result.then(intendedToReturnContentInPromise)
: donePromise

function intendedToReturnContentInPromise(content) {
if (content) {
return content
} else {
return donePromise
}
}
}

async function runProcessors (preprocessors, file, content) {
Expand Down

0 comments on commit 4620550

Please sign in to comment.