【问题标题】:conditional promise chaining multiple levels [duplicate]链接多个级别的条件承诺[重复]
【发布时间】:2019-03-15 15:11:42
【问题描述】:

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

假设我有:

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。实现这一点的最优雅的方法是什么?谢谢

【问题讨论】:

  • 直接将最后两个延续添加到promise2。否则,您将不得不使用 async 和 await 有条件地在 else 块内链接。

标签: javascript promise


【解决方案1】:

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

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;
}

【讨论】:

  • 同意@libik async/await 似乎是正确的(我将把它标记为答案),除了我忘了提到这个函数应该是调用者的一个承诺(它是一个谷歌行动项目)
【解决方案2】:

你有两个选择。您可以像这样将链直接附加到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);
});

或者您可以在第一个延续中使用async 和await:

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)
});

【讨论】:

    【解决方案3】:

    在这里使用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
        }
    })();
    

    编辑: sn-p 到 async 函数中。

    【讨论】:

    • 删除关于 async 函数的注释,只需编辑 sn-p!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 2018-02-25
    • 1970-01-01
    • 2019-07-29
    • 2021-08-01
    • 2018-07-26
    • 1970-01-01
    相关资源
    最近更新 更多