The usage of promise

  node.js, question

Doubt 1:

Most of the program’s call chain looks like this:

promise.then(A).then(B).then(C).then(D)

But B meets certain conditions and needs to be called as follows:

promise.then(A).then(B).then(C1).then(C2).then(D)

How should this be done?


New instructions:

This condition is not determinable at the periphery.

For example:

//This cannot be done.  State cannot be determined until the B function is executed.
 if(state){
 promise.then(A).then(B).then(C1).then(C2).then(D)
 }else{
 promise.then(A).then(B).then(C).then(D)
 bracket

Doubt 2:

How do I use promise in a loop?

Doubt 1

Amendment:

promise
 .then(A)
 .then(B)
 .then(function(state){
 if(state){
 return C1.then(C2);
 bracket
 return C;
 })
 .then(D);

I don’t seem to be handsome either. I am looking forward to a better performance.

Doubt 2

If it occurs in a circular inside, for example, if an array is traversed, the result of processing each element is onePromise, can write like this:

var userIds = ['aaa', 'bbb', 'ccc'];
 
 //here getUserById returns Promise.
 var promises = arr.map(userIds => getUserById(userId));
 
 Promise
 .all(promises)
 .then(function(users) {
 console.log(users);  //Here is the list of users
 });