有条件的诺言链接多个级别

时间:2019-03-15 15:11:42

标签: javascript promise

我已经检查了一些建议的条件承诺链方法,但是我找不到适用于我的用例的解决方案。因此,发布了一个新问题。

假设我有

const promise1 = param1 => {
  return new Promise(resolve => {
    // do something with param1;
    resolve(..);
  });
}

// similarly I have other promises
const promise2 ...

const promise 3 ...

// now I want to call with the following case
const param1 = ...;
promise1(param1)
.then(resp1 => {
  if(resp1 == something) {
    ...
    console.log(resp1) // I want to end here if condition is met
  }
  else {
    ...
    return promise2
  }
})
.then(resp2 => {
   ....
  return promise3
})
.then(resp3 => {
  ....
  console.log(resp3) // else end here
}).
.catch(err => {
  console.log(err)
});

在上面,如果promise1返回一个特定的值,我不想继续进行promise2和3。实现这一目标的最优雅的方法是什么?谢谢

3 个答案:

答案 0 :(得分:0)

您有两个选择。您可以像这样将链直接附加到promise2

const promise1 = param1 => {
  return new Promise(resolve => {
    // do something with param1;
    resolve(...);
  });
};

// similarly I have other promises
const promise2 = ...;

const promise3 = ...;

// now I want to call with the following case
const param1 = ...;
promise1(param1).then(resp1 => {
  if(resp1 == something) {
    ...
    console.log(resp1); // I want to end here if condition is met
  } else {
    ...
    return promise2.then(resp2 => {
      ...
      return promise3;
    }).then(resp3 => {
      ...
      console.log(resp3); // else end here
    });
  }
}).catch(err => {
  console.log(err);
});

或者您可以在第一个延续中使用asyncawait

const promise1 = param1 => {
  return new Promise(resolve => {
    // do something with param1;
    resolve(...);
  });
};

// similarly I have other promises
const promise2 = ...;

const promise3 = ...;

// now I want to call with the following case
const param1 = ...;
promise1(param1).then(async resp1 => {
  if(resp1 == something) {
    ...
    console.log(resp1); // I want to end here if condition is met
  } else {
    ...
    const resp2 = await promise2;
    ...
    const resp3 = await promise3;
    ...
    console.log(resp3); // else end here
  }
}).catch(err => {
  console.log(err)
});

答案 1 :(得分:0)

对此,承诺链并不优雅。您必须预先指定整个流程:

promise1(param1)
.then(resp1 => {
  if(resp1 == something) {
    ...
    console.log(resp1) // I want to end here if condition is met
  }
  else {
    ...
    return promise2.then(resp2 => {
      ....
      return promise3
    })
    .then(resp3 => {
      ....
      console.log(resp3) // else end here
     })
  }
})
.catch(err => {
  console.log(err)
});

如果您可以使用async / await,它将更加优雅

async function doSomething(){
    const resp1 = await promise1(param1);
    if(resp1 == something) {
      return resp1;
    }

    const resp2 = await createPromise2(resp1);
    const resp3 = await createPromise3(resp2);
    return resp3;
}

答案 2 :(得分:0)

在这里使用async/await是最好的解决方案,因为实际上您需要的是同步:

(async () => {
    const promise1 ...
    const promise2 ...
    const promise3 ...

    const resp1 = await promise1(param1);
    if (resp1 == something) {
       console.log(resp1) // I want to end here if condition is met
    } else {
        const resp2 = await promise2;
        const resp3 = await promise3;

        console.log(resp3) // else end here
    }
})();

编辑:摘录到async函数中。