-2

I am trying to run to promises (which make some rest API calls on each). Based on the result of the two promises, use the third function to process the data. Also, the functions running in sequence can be changing. So I made them in a dynamic way of constructing promise.

However, the promises seem to be starting without the previous one ending. Here is a simplified model for testing the same concept.

var funcs = [
 () => {
    console.log('Task 1 Sart =', new Date());
   sleeper(6000, 'Task 1  Resolved');
    console.log('Task 1  Return');
  },
  () => {
  console.log('Task 2  Sart=', new Date());
   sleeper(5000, 'Task 2  Resolved');
    console.log('Task 2  Return');
  },
  () => {
  console.log('Task 3  Start=', new Date());
  console.log('Task 2  Return');
  }
];

function sleeper(m, msg) {
 return new Promise(function(resolve, reject) {
   setTimeout(function() {
    resolve(console.log(msg, new Date()));
   }, m);
 });
}

function runSequence(functionSequence) {
   return functionSequence.reduce((p, func) => {
       return p.then(func);
   }, Promise.resolve());
} 

var v = runSequence(funcs);

The results look like this VM128:51 Task 1 Sart = Wed Jun 07 2017 13:54:34 GMT-0700 (PDT) VM128:53 Task 1 Return VM128:56 Task 2 Sart= Wed Jun 07 2017 13:54:34 GMT-0700 (PDT) VM128:58 Task 2 Return VM128:61 Task 3 Start= Wed Jun 07 2017 13:54:34 GMT-0700 (PDT) VM128:62 Task 2 Return VM238:69 Task 2 Resolved Wed Jun 07 2017 13:54:39 GMT-0700 (PDT) VM238:69 Task 1 Resolved Wed Jun 07 2017 13:54:40 GMT-0700 (PDT)

I would assume I don't see the second task start till the first one ended. Looked like they all started in sequence. Is anything I missed or totally misunderstand how the promises work. What I tried to achieve is to have the first one completed and then the following starts

Sam Tsai
  • 373
  • 1
  • 2
  • 15
  • 1
    No `Promise` or other value is returned from functions in `funs` array, i.e.g, `return sleeper(6000, 'Task 1 Resolved');` – guest271314 Jun 07 '17 at 21:14
  • 1
    Ok. I see what you are pointing out. With a return it will do something in sequence – Sam Tsai Jun 07 '17 at 22:34
  • _"The question is not much about where the value get returned."_ Yes, it is. `return` the `Promise` from the function, else there is no way to determine that the task has been completed at the anonymous function call which performs an asynchronous procedure. You are close, though `sleeper()` call, where you have returned a `Promise`, is not returned from anonymous function call. See also https://stackoverflow.com/questions/44380582/wait-for-node-js-stream-readable-pipe-to-complete? – guest271314 Jun 07 '17 at 22:36
  • https://jsfiddle.net/p4f5oukr/ – guest271314 Jun 07 '17 at 22:46
  • @guest271314 whatever the merits of this question (and it ain't great), it's pretty clearly not an exact duplicate of the one you've linked to. The accepted answer at the link doesn't even contain an example of returning a promise from a `.then()` callback (which is what's needed here), and even if it did that wouldn't be sufficient to make this a dupe. – Mark Amery Jun 07 '17 at 23:12
  • You can use execute your slow functions mixed with logic and recursion in sequential manner by sequential JS executor [nsynjs](https://github.com/amaksr/nsynjs/). Please see example here: https://github.com/amaksr/nsynjs/blob/master/examples/browser-ajax-seq.js – amaksr Jun 07 '17 at 23:28
  • 1
    @amaksr A library is not necessary to meet requirement. Only `return`, and possibly a chained `.then()` is necessary, if the second `console.log()` call is expected to be called after `Promise` returned from `sleeper()` call is resolved, as demonstrated at linked jsfiddle. – guest271314 Jun 07 '17 at 23:30
  • @MarkAmery Updated links to duplicate Questions https://stackoverflow.com/questions/43965421/javascript-cant-get-the-value-of-the-function-outside-of-it-getting-undefine, which ironically, was also marked as a duplicate to initial duplicate Question. Also, the working code of what OP is trying to achieve is at linked jsfiddle at https://stackoverflow.com/questions/44422851/run-javascript-promises-in-order-one-after-the-other-ends#comment75846292_44422851 – guest271314 Jun 07 '17 at 23:39
  • @guest271314 still not a duplicate; the question you're linking to is about passing values through a chain of promises, while this one is about making them run sequentially. – Mark Amery Jun 08 '17 at 07:43
  • @MarkAmery What occurs with the `Promise` value is inconsequential to the issue at current Question. The gist of issue at Question is the the `Promise` object is not `return`ed from the anonymous function calls within `funcs` array, as described at each link a duplicate Questions _"`return`"_ is mentioned at each link – guest271314 Jun 08 '17 at 13:35
  • The actual case is to run sequence of the redux dispatched functions. In that case it is not waiting till last one is done. The function got a return in its body. Maybe I am missing something on redux dispatch – Sam Tsai Jun 08 '17 at 14:12

1 Answers1

-2

Based on the comments. Here is the version works. A simple return is missing from original codes

var funcs = [
 () => {
    console.log('Task 1 Sart =', new Date());
   return sleeper(7000, 'Task 1  Resolved');    
  },
  () => {
   console.log('Task 2  Sart=', new Date());
   return sleeper(3000, 'Task 2  Resolved');
  },
  () => {
  console.log('Task 3  Start=', new Date());
  console.log('Task 3  Return');
  }
];

function sleeper(m, msg) {
 return new Promise(function(resolve, reject) {
   setTimeout(function() {
    resolve(console.log(msg, new Date()));
   }, m);
 });
}

function runSequence(functionSequence) {
   return functionSequence.reduce((p, func) => {     
     return p.then(func);
   }, Promise.resolve());
} 

runSequence(funcs);
Sam Tsai
  • 373
  • 1
  • 2
  • 15