首页 > 试题广场 >

如果已经有三个promise,A、B和C,想串行执行,该怎

[问答题]

如果已经有三个promise,A、B和C,想串行执行,该怎么写?

function one(){
  console.log(11111);
}

function two(){
  console.log(22222);
}

function three(){
  console.log(33333);
}

function fiveP(func){
  return new Promise(function(resolve, reject) {
    func();
    resolve();
  });
}

p.then(fiveP(one)) /* 11111 */
.then(fiveP(three)) /* 33333 */
.then(fiveP(two)) /* 22222 */
.then(function(result) {
  console.log('Got value: 最后执行' + result); // Got value:最后执行
});

编辑于 2021-10-06 17:39:11 回复(0)
function A() {
  new Promise((resolve, reject) => {
      resolve("data");
      reject("info")
  })
}
A().then(
    data => {
        console.log(data);
        var B = new Promise((resolve, reject) => {
            resolve("data");
            reject("info")
    })
        return B;
    }
 ).then(
    data => console.log(data),
    new Promise((resolve, reject) => {
      resolve("data");
      reject("info")
  })
 )
发表于 2020-11-07 18:27:14 回复(0)