Waiting for promises to resolve before running next job?
- Dominant language
- JavaScript
- Stars
- 9.4k
- Forks
- 858
- PR merge metrics
- No merged PRs in 30d
Description
My application is structured somewhat like this:
`
router.get('/', (req, res) => {
const node = req.body.node;
const operation = req.body.operation;
const value = req.body.value;
const ids = req.body.ids;
queue
.create(node, {
title: "Pausing",
data: {
node,
operation,
value,
ids
}
})
.priority("high")
.save();
queue.process(node, (job, done) => {
axios.get(`https://backend-service.com/test`, job.data)
.then(res => {
console.log(res.status);
done();
})
});
res.send("Hello World!");
});
`
Here's what the backend-service test endpoint is:
`
router.get('/test', async (req, res, next) => {
const p = new Promise(resolve => {
setTimeout(() => {
console.log(req.body);
resolve();
}, 10000);
});
await p;
return res.sendStatus(200);
});
`
The issue is that when I call this endpoint twice, with the same node parameter, what happens is it'll wait 10 seconds, then it'll console.log the response status twice right after one another.
What I want to happen is once the API is called twice, the first job should run, wait 10 seconds, then complete, then the second job should begin. Right now, both jobs are beginning at the same time essentially.
Contributor guide
No contributing guide indexed for this repository
Research direction
Reproduce the behavior by submitting two jobs with the same node and observing queue.process alongside the axios request and done callback. Read the queue.process and job completion behavior first; done should mark the first job complete before the second begins, and the reproduction should show whether the queue is running both jobs concurrently.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100