Promise.race中断Promise

场景:Promise设置超时时间

const wrap = promise => {
  let abort
  let myP = new Promise((resolve, rejcet) => {
    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('timeout'), 2000)

你可能感兴趣的:(Promise.race中断Promise)