Skip to content

Commit

Permalink
async/await -> .then()/.catch() (#4)
Browse files Browse the repository at this point in the history
async/await -> .then()/.catch()
  • Loading branch information
wardpeet authored Jan 19, 2020
2 parents 7a2a163 + caf701a commit 3c8664c
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions packages/gatsby-plugin-sharp/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,28 +146,40 @@ function prepareQueue({ file, args }) {
}
}

async function createJob(job, { reporter }) {
function createJob(job, { reporter }) {
const progressBar = createOrGetProgressBar(reporter)

if (pendingImagesCounter === 0) {
progressBar.start()
}

const transforms = job.args.operations
pendingImagesCounter += transforms.length
const transformsCount = job.args.operations.length
pendingImagesCounter += transformsCount
progressBar.total = pendingImagesCounter

try {
if (boundActionCreators.createJobV2) {
await boundActionCreators.createJobV2(job)
} else {
await scheduleJob(job, boundActionCreators)
}
} catch (err) {
reporter.panic(err)
// Jobs can be duplicates and usually are long running tasks.
// Because of that we shouldn't use async/await and instead opt to use
// .then() /.catch() handlers, because this allows V8 to release
// duplicate jobs from memory quickly (as job is not referenced
// in resolve / reject handlers). If we would use async/await
// entire closure would keep duplicate job in memory until
// initial job finish.
let promise = null
if (boundActionCreators.createJobV2) {
promise = boundActionCreators.createJobV2(job)
} else {
promise = scheduleJob(job, boundActionCreators)
}

progressBar.tick(transforms.length)
promise
.catch(err => {
reporter.panic(err)
})
.then(() => {
progressBar.tick(transformsCount)
})

return promise
}

function queueImageResizing({ file, args = {}, reporter }) {
Expand Down

0 comments on commit 3c8664c

Please sign in to comment.