Run JavaScript promises in order. One after the other ends [duplicate]
Asked Answered
F

1

-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

Feathered answered 7/6, 2017 at 21:12 Comment(11)
No Promise or other value is returned from functions in funs array, i.e.g, return sleeper(6000, 'Task 1 Resolved');Brianna
Ok. I see what you are pointing out. With a return it will do something in sequenceFeathered
"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 #44381082?Brianna
jsfiddle.net/p4f5oukrBrianna
@Brianna 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.Autocatalysis
You can use execute your slow functions mixed with logic and recursion in sequential manner by sequential JS executor nsynjs. Please see example here: github.com/amaksr/nsynjs/blob/master/examples/…Radiophone
@Radiophone 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.Brianna
@MarkAmery Updated links to duplicate Questions #43965921, 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 #44423351Brianna
@Brianna 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.Autocatalysis
@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 returned from the anonymous function calls within funcs array, as described at each link a duplicate Questions "return" is mentioned at each linkBrianna
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 dispatchFeathered
F
-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);
Feathered answered 8/6, 2017 at 16:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.