promise和async中断或取消的办法

博客介绍了promise和async取消的办法,即在相应情况下通过return一个空的promise来实现取消,还指出直接return一个false是无效的,且分别证明了该方法在async和promise情况下均有效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

promise和async取消的办法:return 一个空的promise

直接return 一个false是没有效果的

async fn1() {
  await articleItems().then(res => {return false})
  await articleItems().then(res => {console.log(3)})
  console.log(4)
}
//3,4

这是在async的情况下,证明有效

   async fn1() {
      await articleItems().then(res=>{console.log(1); return new Promise(() => {})}).then(res=>articleItems().then(res => {console.log(5)}))
      await articleItems().then(res => {return false})
      await articleItems().then(res => {console.log(3)})
      console.log(4)
    }
//1

这是在promise的情况下,证明有效

  articleItems().then(res => {
    if (true) {
      console.log(1)
      return new Promise(() => {});
    }
  }).then(res => {
      console.log("2res");
    });
//1,res
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值