kue
kue copied to clipboard
use Promises for tasks
We're writing majority of code using async/await these days. So our first thought was to use it for kue-tasks too. Something like this:
queue.process('hello', async (job) => {
let tmpResult = await someLongBoringOperation(job.data.name);
await otherLongOperation(tmpResult, job.data.planet);
});
That doesn't work as kue doesn't distinguish promises from regular functions. Workarounds are mentioned in Readme, but they hurt elegance of app code.
Is there a chance that kue will have "native" support for promises?
The only complication I see is optional workerCtx
param. Otherwise, the code should be trivial
I'm thinking of re-writing Kue 1.0 with bluebird
and so any contributions are welcome :+1:
Why not standard es6 promises? I personally find it slightly annoying when third party modules use their own promise implementation.
:+1:
+1
Why not standard es6 promises?
I'm not sure if ES6 restriction is a good choice for Kue users, of which many may be on lower node.js versions currently!
@behrad there's https://github.com/stefanpenner/es6-promise or https://github.com/zloirock/core-js... Or going all in with a babel compilation step before publishing on the npm registry.
@olalonde http://programmers.stackexchange.com/questions/278778/why-are-native-es6-promises-slower-and-more-memory-intensive-than-bluebird
@behrad +1 for bluebird
I would suggest using https://github.com/kevinbeaty/any-promise This lets users choose which Promise implementation they prefer.
I think you should do something like this line
https://github.com/glazedio/frisbee/blob/master/src/frisbee.js#L15
so the user can use the polyfill he wants,
https://github.com/matthew-andrews/isomorphic-fetch is a good option because works on browser and on node
I would suggest using https://github.com/kevinbeaty/any-promise This lets users choose which Promise implementation they prefer.
👍
Promises are an ECMAScript standard now however... you don't see that many npm modules using any-array
or equivalent to let users chose which Array implementation they prefer.
Would love to see standard promises used. Authoring my nodejs code in TypeScript these days, so I'm definitely at the "await
buffet"!
Just in-case someone if looking for promisifying the current version of Kue with Bluebird
var kue = require('kue');
var bPromise = require('bluebird');
bPromise.promisifyAll(kue.Job.prototype);
bPromise.promisifyAll(kue.Job);
var queue = kue.createQueue(); // kue.createQueue() returns a singleton instance of queue object
// so its safe to call careateQueue() multiple times in your code
bPromise.promisifyAll(queue.constructor.prototype);
Are there any plans to support promises? I'd love to help implementing that if it is something you guys want to add
Bluebird does not seem to correctly promisify queue.process
method:
Bluebird.promisifyAll(kue.prototype)
But surely it can be done manually with something like:
const kue = require('kue')
const queue = kue.createQueue(options)
kue.prototype.processAsync = (name, concurrency, handler) => {
return this.queue.process(name, concurrency, (job, done) => {
return handler(job).then(() => done(null)).catch(done)
})
}
And used as follows:
const queue = kue.createQueue(options)
queue.processAsync('myJob', 1, async job => {
await Promise.resolve('one')
await Promise.resolve('two')
return Promise.reject(new Error('Not implemented'))
})
Can someone explain what the above method does? + It doesn't seem to be working for me, is anyone else having issue's?
@notflip if you look at the proposal it must not to be working, since it's just a proposal. What are you doing and what are you trying to achieve?