0

I am pretty new to Nodejs and come from Java background. I apologize for the long question. I am working on an standalone Nodejs application which has to do steps sequentially. Below are the steps:

Step1: The application has to call the external-server-A or else retry

Step2: Once the above call is success, it has to call external-server-b by taking the response on Step1 or else retry.

Step3: Once the above call is success, it has to invoke local module by taking the response-of-step2 and call a function.

Not to combine all the steps in 1 JS page, I would like to write the functions related to above steps in different JS-pages and import them via require(). I am not sure how to call them sequentially. Should I have the require(./step2), require(./step3) in the call-back code-block of step1 function and step2 functions.

Thanks in advance for the help.

John
  • 121
  • 6
  • 18

2 Answers2

2

You will want to require step2 and step3 at the top of your page, but expose them as a function which you can execute at a later time. You can also use promises to help you with writing your async code. For example:

// Step one
const step2 = require('./step2');
const step3 = require('./step3');

function someAsyncCallToExternalServerA() {
  /*
    Return a promise here which resolves to when
    your call to external server A call is successful
  */
}

someAsyncCallToExternalServerA()
  .then(function(serverAResults) { // I don't know if you need the serverA results or not

    // This will pass the results from the step2 success to the step3 function
    return step2.then(step3);
  })
  .then(function() {
    console.log('All done!');
  })
  .catch(function(err) {
    console.log('Failed: ', err);
  })
Andrew Lively
  • 2,145
  • 2
  • 20
  • 27
  • Andrew Lively: Thanks for the info. I will read more about Promises. For retrying with promises what is the best approach. – John Jul 11 '17 at 05:47
  • 1
    @John I would recommend checking out this question for retrying promises: https://stackoverflow.com/questions/38213668/promise-retry-design-patterns – Andrew Lively Jul 11 '17 at 12:52
  • If I have to call these steps from app.js file and Step1's response has to pass to Step2 then how can I make this. – John Jul 12 '17 at 08:13
1

One way to go is to use various callbacks to trigger what you want, when you want.

Imagine two steps:

function step1(onSuccess, onError) {
     iDoSomethingAsync(function (err) {
         if (err) onError()
         else onSuccess()
     }
}
function step2(onSuccess, onError) {
     iDoSomethingElseAsync(function (err) {
         if (err) onError()
         else onSuccess()
     }
}

Then you can simply chain calls like that:

step1(step2, step1)

step 1 is called, do something, if something return no error, it'll call step2. If we are in error it'll call step1 again.

In async programming, you have to understand that when calling someFunc(callback), someFunc HAVN'T finished his job in the next line. But somefunc WILL HAVE finished his job when callback is called.

It's up to you to do whatever you want with callback, because you are guaranted that the function has done his work (or errored)

Taking back the step1/step2 example, here is the same function, calling back step1 with 1 sec delay in case of an error:

step1(step2, setTimeout.bind(this, step1, 1000))

Once you think in the correct way, it's pretty simple isn't it? If you are coming from java, consider it's a mix between lambdas, Tasks and Futures/Promises.

Also, as the other answer pointed, using a library like promise will help you write cleaner code (and I recommend it as my example isn't clean at all), but you still need to understand how everything works.

Atrakeur
  • 4,126
  • 3
  • 17
  • 22
  • Thanks for your detailed explanation, it really helped. Also about chaining the calls and setting the retry mechanism. Thank you very much for the Info. – John Jul 11 '17 at 05:24