Created
July 19, 2020 01:47
-
-
Save zhanglun/11ddf7c3ea522dd3b7d388d060e1f48c to your computer and use it in GitHub Desktop.
wrapPromiseWithAbort.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let wrapPromiseWithAbort = function (promise) { | |
| let _abort = null | |
| let pAbort = new Promise ((resolve, reject) => { | |
| _abort = function (reason = 'abort!') { | |
| console.warn(reason) | |
| rej(reason) | |
| } | |
| }) | |
| let race = Promise.rece([promise, pAbort]) | |
| race.abort = _abort | |
| console.log(priomise, pAbort) | |
| return race | |
| } | |
| let p1 = new Promise((res) => { | |
| setTimout(() => { | |
| res('p1 success') | |
| }, 2000) | |
| }) | |
| let testP = PromiseWithAbort(p1) | |
| testP.then((res) => { | |
| console.log('success: ', res) | |
| }, (error) => { | |
| console.log('error: ', error) | |
| }) | |
| testP.abort() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment