最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to run background task in node.js api after sending response? - Stack Overflow

programmeradmin1浏览0评论

I have a requirement, where i have to run a one minute background process after returning the response for an api. That background process will do some operation on mongodb.

My approach is, i am emitting an event for background process after returning the response.

Is there any best approach to this operation? Please help me.

Thanks,

I have a requirement, where i have to run a one minute background process after returning the response for an api. That background process will do some operation on mongodb.

My approach is, i am emitting an event for background process after returning the response.

Is there any best approach to this operation? Please help me.

Thanks,

Share Improve this question asked Mar 6, 2018 at 19:06 aaqib90aaqib90 5482 gold badges5 silver badges19 bronze badges 1
  • where you able to accomplish that? – vir us Commented Jun 20, 2020 at 12:24
Add a comment  | 

3 Answers 3

Reset to default 13

You could use an EventEmitter to trigger the background task.

Or you can trigger an asynchronous task before you return the response.

I would implement some kind of simple in-memory queue. Before returning the response I would add a task to the queue, emit an event telling listeners there is task in the queue.

Edit:

I'm not sure if I understand your use case exactly. But this might be one approach.

If you do not have reference to do the mongo you might have to do some fast lookup or creation, then return the response, then run the task

const myqueue = []

const eventHandler = new EventEmitter();

eventHandler.on('performBackgroundTask', () => {

  myqueue.forEach(task => {
    // perform task
  })

})

app.get('/api', function (req, res) {

    const identificationForItemInMongo = 123

    myqueue.push(identificationForItemInMongo)

    eventHandler.emit('performBackgroundTask',     identificationForItemInMongo)

   res.send('Send the response')
})

when you want to make asynchronous calls to db and wait for the result you need to use callback or using promises or async/await in ES6.

read this for more info

You can use Promise chaining for your approach. Call first Api, once response is received display the value in the UI then second call will haven automatically and it will not interfere any UI process. You can refer more details about promise chaining here.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

var promise1 = new Promise(function(resolve, reject) {
  resolve('Success!');
});

//Run the background process.
var promise2 = new Promise(function(resolve, reject) {
  resolve('Success!');
});

promise1.then(function(value) {
  console.log(value);
  // expected output: "Success!"
  return promise2;
}).then(function(value){
  // Response for the second process completion.
}).catch(function(){
  // Failure for first api call/ second api call
});
发布评论

评论列表(0)

  1. 暂无评论