终止Promise的执行
创始人
2025-01-08 20:36:23
0

文章目录

  • 终止Promise的执行
    • 1. HTML部分
    • 2. JavaScript部分
      • 1. Promise.race()
      • 2. Promise.withResolvers()
      • 3. AbortController类

终止Promise的执行

1. HTML部分

                  Document                 

2. JavaScript部分

1. Promise.race()

let abort = null; let p1 = new Promise((resolve, reject) => {     setTimeout(() => {         console.log("p1");         resolve('p1');     }, 3 * 1000) })  let p2 = new Promise((resolve, reject) => {     abort = reject;     setTimeout(() => {         console.log('p2');         resolve('p2')     }, 10 * 1000) })  Promise.race([p1, p2]).then(res => {     console.log(res) }).catch((err) => {     console.log(err, 'abort') })  btn.addEventListener('click', () => {     abort('abort') }) 

2. Promise.withResolvers()

// 模拟异步任务 const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));  const controlPromise = (asyncFn) => {     let rejected = false;     let { promise, resolve, reject } = Promise.withResolvers();      return {         abort: () => {             rejected = true;             reject(new Error('Task abort'));         },         start: () => {             if (!rejected) {                 asyncFn().then(resolve, reject);             };             return promise;         }     } }  const task = controlPromise(async () => {     await sleep(10 * 1000);     return 'task done'; }); task.start().then(console.log, console.error);  btn.addEventListener('click', () => {     task.abort(); })  

3. AbortController类

// 模拟异步任务 const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));  const controlPromise = (asyncFn) => {     const abortController = new AbortController();     const signal = abortController.signal;     return {         abort: () => abortController.abort(),         start: () => {             return new Promise((resolve, reject) => {                 const cancel = () => reject(new Error('Task abort'));                 if (signal.aborted) {                     cancel();                     return;                 }                 asyncFn().then(resolve, reject);                 signal.addEventListener('abort', cancel);             })         }     } } const task = controlPromise(async () => {     await sleep(10 * 1000);     return 'task done'; }); task.start().then(console.log, console.error);  btn.addEventListener('click', () => {     task.abort(); }) 

相关内容

热门资讯

透视科技!werplan外挂,... 透视科技!werplan外挂,衢州都莱辅助软件(好像是真的有挂)1、游戏颠覆性的策略玩法,独创攻略技...
透视智能ai“aapoker透... 透视智能ai“aapoker透视脚本安装包”都是存在有挂(辅助挂)详细辅助揭秘教程;1、很好的工具软...
wepoker辅助器怎么用!w... wepoker辅助器怎么用!wpk真吗,德普之星辅助正版(详细辅助力荐教程);无聊就玩这款wepok...
透视实锤!wpk透视表,hhp... 透视实锤!wpk透视表,hhpoker透视脚本(详细辅助揭秘教程);科技安装教程;136704302...
透视存在!hhpoker是真的... 透视存在!hhpoker是真的假的,闲聚鱼虾蟹软件脚本辅助器(一贯存在有挂);1.闲聚鱼虾蟹软件脚本...
透视透视“aapoker免费透... 透视透视“aapoker免费透视脚本”本来有挂(辅助挂)详细辅助安装教程;软件透明挂微扑克wpk插件...
德普之星辅助器app!智星德州... 德普之星辅助器app!智星德州插件最新版本更新内容详解,德普之星透视辅助插件(详细辅助力荐教程)是一...
透视好友房!aapoker辅助... 透视好友房!aapoker辅助工具免费下载,wepoker怎么增加运气(详细辅助玩家教程);支持2-...
透视插件!fishpoker透... 透视插件!fishpoker透视,蜀山四川辅助(切实是有挂)1、不需要AI权限,帮助你快速的进行蜀山...
透视苹果版“红龙poker作弊... 透视苹果版“红龙poker作弊指令”原来有挂(辅助挂)详细辅助攻略方法;1、系统规律教程、辅助透视等...