kue icon indicating copy to clipboard operation
kue copied to clipboard

use Promises for tasks

Open indeyets opened this issue 8 years ago • 17 comments

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

indeyets avatar Dec 25 '15 13:12 indeyets

I'm thinking of re-writing Kue 1.0 with bluebird and so any contributions are welcome :+1:

behrad avatar Dec 25 '15 17:12 behrad

Why not standard es6 promises? I personally find it slightly annoying when third party modules use their own promise implementation.

olalonde avatar Mar 01 '16 23:03 olalonde

:+1:

sibelius avatar Mar 04 '16 14:03 sibelius

+1

Paveltarno avatar Mar 06 '16 15:03 Paveltarno

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 avatar Apr 02 '16 08:04 behrad

@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 avatar Apr 02 '16 20:04 olalonde

@olalonde http://programmers.stackexchange.com/questions/278778/why-are-native-es6-promises-slower-and-more-memory-intensive-than-bluebird

@behrad +1 for bluebird

StrikeForceZero avatar Apr 25 '16 18:04 StrikeForceZero

I would suggest using https://github.com/kevinbeaty/any-promise This lets users choose which Promise implementation they prefer.

jltrv avatar Jun 13 '16 14:06 jltrv

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

sibelius avatar Jun 13 '16 14:06 sibelius

I would suggest using https://github.com/kevinbeaty/any-promise This lets users choose which Promise implementation they prefer.

👍

behrad avatar Jun 14 '16 14:06 behrad

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.

olalonde avatar Jun 15 '16 07:06 olalonde

Would love to see standard promises used. Authoring my nodejs code in TypeScript these days, so I'm definitely at the "await buffet"!

atrauzzi avatar Feb 12 '17 13:02 atrauzzi

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);

jaladankisuresh avatar May 28 '17 12:05 jaladankisuresh

Are there any plans to support promises? I'd love to help implementing that if it is something you guys want to add

santiagodoldan avatar Jul 19 '17 20:07 santiagodoldan

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'))
})

ruchevits avatar Nov 15 '17 17:11 ruchevits

Can someone explain what the above method does? + It doesn't seem to be working for me, is anyone else having issue's?

notflip avatar Jan 05 '19 14:01 notflip

@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?

StreetStrider avatar Jan 05 '19 18:01 StreetStrider