-1

I need to execute 3 functions (all include a callback function inside), one after the other. I need a simple solution, with the least number of lines, that explains how to do it. No HTML code please. Only Javascript ES6.

An example to explain my problem.

function myFunction() {
    doJob1() { /* Callback function includes */ }
    doJob2() { /* Callback function includes */ }
    doJob3() { /* Callback function includes */ }
}
  • In your example it isn't clear what problem this would present, when you call myFunction, it will call the doJob1(), doJob2() and doJob3() routines in the sequence they defined in the function, whats the problem? – SPlatten Sep 02 '19 at 07:31
  • 2
    Your question is really not clear, but I have a feeling you're looking for [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) ([More details here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises)) – Seblor Sep 02 '19 at 07:33
  • 1
    The minimal solution would be `doJob1(() => doJob2(doJob3))`, but we really need more explanations. – georg Sep 02 '19 at 07:34
  • 1
    Check out [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) – eibersji Sep 02 '19 at 07:36
  • What I meant was doJob1 etc. contains asynchronous functions. They are actually reading & writing to a database, but the activities should happen in the order. e.g doJob1 reading a field from one Table, and doJob2 write that to another Table, etc. I hope you get the picture. – Harsha Wickramasinghe Sep 02 '19 at 08:24

3 Answers3

0

As stated in comments by Seblor, georg, eibersji you can do it with promises, or pass functions in Your order as callbacks (a little bit ugly but it will work). But if this is Your only problem then great and detailed answers for such questions already exist in stackoverflow:

https://stackoverflow.com/a/5188020/9978135 (callbacks)

https://stackoverflow.com/a/32733694/9978135 (promises)

Jacck Mark
  • 127
  • 1
  • 10
0

The problem seems not to be clear, but let's assume you want

doJob1() { /* Callback function includes */ } to execute this function first

doJob2() { /* Callback function includes */ } to execute second, after doJob1() is completed

doJob3() { /* Callback function includes */ }at last this doJob3() to execute.

Here then instead of executing line by line, execute 2nd function doJob2() after only doJob1() using Promises(precisely to be Promise Chaining). Similarly, doJob3() after doJob2() is completed.

new Promise(function(resolve, reject) {

  //Code for executing function doJob1()

}).then(function(result) {

  #when doJob1() is finished.

  return new Promise((resolve, reject) => {
     //Code for executing function doJob2()
  });

}).then(function(result) {  
  #when doJob2() is finished.
  return new Promise((resolve, reject) => {
    //Code for executing function doJob3()
  });

}).then(function(result) {

 #when doJob3() is finished.

});

About Promises go through

About Promises

About Promises Chaining Promises Chaining

Not A Bot
  • 2,474
  • 2
  • 16
  • 33
0

Thanks Not-A-Bot, for your help. Accordingly, This is the code I wanted as the solution.

new Promise(function(resolve, reject) {

    doJob1(() => resolve(1));

}).then(function(result) {

    console.log('Job1 completed',result); // 1
    return new Promise((resolve, reject) => { // (*)
        doJob2(() => resolve(result * 2 ))
    });

}).then(function(result) { // (**)

    console.log('Job2 completed',result); // 2
    return new Promise((resolve, reject) => {
        doJob3(() => resolve(result * 2 ))
    });

}).then(function(result) {

    console.log('Job3 completed',result); // 4

});

function doJob1 ( para ) {     // para is '() => resolve(1)'
    setTimeout(function(){ para() }, 1000); // u may have your 
}                                         // own code here

function doJob2 ( para ) {
    setTimeout(function(){ para() }, 3000); // u may have your 
}                                         // own code here

function doJob3 ( para ) {
    for(var i = 0; i < 999999999; i++); // some different code doing 
    if( i > 0)                          // similar thing as setTimeout  
    para();
}