Created
June 8, 2021 13:43
-
-
Save ZWkang/c639a053ea38fa7c1a30ad94a3a81b86 to your computer and use it in GitHub Desktop.
提前终止promise链式调用 stop promise chain run
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 cancel = null; | |
| const canceler = new Cancel((c) => cancel = c); | |
| function Cancel( exc ) { | |
| let token = this; | |
| let pendingPromise = null; | |
| this.promise = new Promise((resolve)=> { | |
| pendingPromise = resolve; | |
| }) | |
| exc(function e(message){ | |
| pendingPromise(message) | |
| }) | |
| } | |
| function Progress(config) { | |
| return new Promise((resolve, reject) => { | |
| if(config.cancelToken){ | |
| config.cancelToken.promise.then((_resolve) => { | |
| reject(123) | |
| }) | |
| } | |
| config.callback(resolve, reject); | |
| }) | |
| } | |
| Progress({ | |
| callback: (resolve, reject) => { | |
| setTimeout(() => { resolve(4000) }, 4000) | |
| }, | |
| cancelToken: canceler, | |
| }).then((val) => { console.log(val) }).catch(reason => console.log('fail',reason)) | |
| setTimeout(() => {cancel()}, 2000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment