slightly_toasted
slightly_toasted

Reputation: 335

How to execute and get result of NodeJS promise?

var Queue = require('bull');

// Init queue
var workQueue = new Queue("workQueue", "redis://pass@ip:6379");

// Add 10 jobs to the queue
for (i=0; i<10; i++) {
    workQueue.add({msg:i});
}

// Get # of jobs in queue
workQueue.count(); // <-- does not return queue job count

According to the documentation, .count()

"Returns a promise that returns the number of jobs in the queue"

If I understand this correctly, a promise is an async process waiting to be executed. So how do I execute this promise and get its result?

Upvotes: 0

Views: 98

Answers (1)

Chiranjib
Chiranjib

Reputation: 1783

If I am guessing your requirement correctly, you're doing queue.count() to determine if there are pending jobs.

You should do a setInterval() to periodically check, and then take action when nothing's pending:

let check = setInterval(()=>{
    workQueue.count().then(_count => {
        if (_count === 0){
            clearInterval(check);
            /* do what you need to do here */
        }
    });
}, 5000);

Upvotes: 1

Related Questions