1

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?

3
  • It is not clear exactly what you're asking for help with. It seems that you would just put these three tasks into their own function and insert that function into your call chain where you want it to happen. If you want the rest of the code to wait for these three tasks to finish, then you return a promise from your function that is not resolved until your task AS is done. If you don't want them to wait, then either return nothing or a resolved promise. Commented Jun 22, 2016 at 11:12
  • I don't want rest of the code to wait for these three tasks. Commented Jun 22, 2016 at 11:44
  • So, just put that code into a function and call that function wherever you want in the sequence. This seems so straightforward, that I don't understand what you are confused about? Commented Jun 22, 2016 at 11:53

1 Answer 1

1

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)
      });
  }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.