nirvair
nirvair

Reputation: 4180

async task in node.js

I have four task that are working in sync.

It looks like this:

function createEntity(req, res, next) {
    validator.validateInternalProductRequest(req.body)
      .then((data) => feedbackSetting.getUrl(data))
      .then((data) => bitlyService.shortenLink(data))
      .then((data) => internalProductService.saveInternalProductRequest(data))
      .then((data) => res.send(data))
      .catch(err => {
        console.log(err);
        next(err)
      });
  }

Now between 3rd and 4th task, that is after getting short link and before saving to the database, I need to perform a task, lets say Task AS that I need to run asynchronously. The 4th task, i.e saving to the database should not be blocked because of this.

Now this task AS that I have to do asynchronously, has further three more task: 1. getting setting from db 2. making curl request 3. saving in the database

These three task I can do using async.waterfall or I am hoping there would be any alternatives to this?

How do I perform this task AS in the above mentioned function createEntity?

Upvotes: 1

Views: 259

Answers (1)

libik
libik

Reputation: 23029

If you do not want to wait for the async task, just call it and dont wait for it. It is easy as that.

function createEntity(req, res, next) {
    validator.validateInternalProductRequest(req.body)
      .then((data) => feedbackSetting.getUrl(data))
      .then((data) => {
          callSomeAsyncMethodAndDontWaitForResult();
          return bitlyService.shortenLink(data)
      }).then((data) => internalProductService.saveInternalProductRequest(data))
      .then((data) => res.send(data))
      .catch(err => {
        console.log(err);
        next(err)
      });
  }

Upvotes: 1

Related Questions