let promise = new Promise((resolve,reject)=>{ setTimeout(() => { resolve('ok 成功了') }, 10000); }) const wrap = promise=>{ let abort let myP = new Promise((resolve,reject)=>{ abort = reject }) let p = Promise.race([promise,myP]) p.abort =abort return p } let p = wrap(promise) p.then(data=>{ console.log(data); },(err)=>{ console.log(err); }) setTimeout(() => { p.abort('promise 超时') }, 2000);
//强制中断 Promise.resolve(100).then().then(()=>{ return new Promise((resolve,reject)=>{ }) }).then(data=>{ console.log(data) },err=>{ console.log(err) })