Skip to content

v5.0.0: Async/Await

Compare
Choose a tag to compare
@evantahler evantahler released this 21 Sep 22:05
· 939 commits to main since this release

Version 5.0 rewrites node-resque to use Async/Await syntax rather than callbacks.

Check out the examples & README for more information on the updated APIs.

Node.js version v8.0.0 and above is now required to run this project.

Breaking Changes

  • Every callback-based method API in the package has changed to become an async method.
    • Errors now throw rather than return an Error object.
    • You can use try/catch to decide what to do with errors in your project
    • for example:
try {
  const queue = new NodeResque.Queue({connection: connectionDetails}, jobs)
  queue.on('error', function (error) { throw error })
  await queue.connect()
} catch (error) {
  console.error(`Queue Connection Error: ${error}`)
}

try {
  await queue.enqueue('math', 'add', [1, 2])
  await queue.enqueue('math', 'add', [1, 2])
  await queue.enqueue('math', 'add', [2, 3])
  await queue.enqueueIn(3000, 'math', 'subtract', [2, 1])
} catch (error) {
  console.error(`Enqueue Error: ${error}`)
}
  • Jobs' perform methods should now be async methods that return a response value, for example:
const jobs = {
  'Sleep Add': {
    plugins: ['JobLock'],
    pluginOptions: {
      JobLock: {}
    },
    perform: async (a, b) => {
      let answer = a + b
      await new Promise((resolve) => { setTimeout(resolve, 1000) })
      return answer
    }
  }
}
  • Plugins now inherit from NodeResque.Plugin, for example:
const NodeResque = require('node-resque')

class MyPlugin extends NodeResque.Plugin {
  beforeEnqueue () {
    // console.log("** beforeEnqueue")
    return true // should the job be enqueued?
  }

  afterEnqueue () {
    // console.log("** afterEnqueue")
  }

  beforePerform () {
    // console.log("** beforePerform")
    return true // should the job be run?
  }

  afterPerform () {
    // console.log("** afterPerform")
  }
}
  • All classes now follow proper convention of having capitol letter names, ie NodeResque.Plugin
  • Plugin lifecycles are now camel case: beforeEnqueue, afterEnqueue, beforePerform, afterPerform
  • Plugin lifecycle methods are now async methods, with no callback (per the above)