Agenda: Add promise support and deprecate callbacks

Created on 19 Jul 2017  路  14Comments  路  Source: agenda/agenda

For starters this would help with the tests being nested 4+ levels deep.

For example.

  afterEach(done => {
    setTimeout(() => {
      jobs.stop(() => {
        clearJobs(() => {
          mongo.close(() => {
            jobs._mdb.close(done);
          });
        });
      });
    }, 50);
  });

Ref for my own use later on: https://futurestud.io/tutorials/callback-and-promise-support-in-your-node-js-modules

discussion enhancement +1 planned

Most helpful comment

All 14 comments

So looks like we just need to wrap the return this in a promise that resolves this.

module.exports = function() {
    const self = this;

    return new Promise(resolve => resolve(self));
};

This allows both of these to work.

const agenda = new Agenda();

agenda.cancel().then(async agenda => {
   console.log('cancelled') ;

   await agenda.stop().close();
});
const agenda = new Agenda();

agenda.cancel(() => {
   console.log('cancelled') ; 

   agenda.stop(() => {
     agenda().close();  
   });
});

@agenda/maintainers thoughts?

馃憤

Seems like a reasonable path forward.

Since all tests are passing after making this change to a few files it looks like we should be fine adding this it breaking anything.

I'd do some more testing but I think we may be able to land this in 1.1.0. 馃檹

Awesome!

We should also support job definitions returning a function or a promise.

Ref: https://github.com/agenda/agenda/issues/508

Please have a look here in the meanwhile https://github.com/agenda/agenda/pull/532

Added a small example that could help inexperienced people like myself.
馃檹 馃帀

Any news on promise support? It can be a very good feature if it can support promises on all methods so you can call agenda functions with async/await.

This is a deal-breaker for me. Any update on when this will be implemented?

@toddhickerson we've been working on it. https://github.com/agenda/agenda/pull/557

@toddhickerson @elias-garcia help testing and reviewing #557 always very much appreciated to move things onwards faster! ;-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Relecto picture Relecto  路  12Comments

abishekrsrikaanth picture abishekrsrikaanth  路  33Comments

lucazulian picture lucazulian  路  21Comments

MMHossaini picture MMHossaini  路  23Comments

viktorzavadil picture viktorzavadil  路  16Comments