-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
79 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,40 @@ | ||
interface LikeWXApi { | ||
success?(res?: any): void; | ||
fail?(res?: any): void; | ||
} | ||
|
||
export default function promisify<T>( | ||
f: (cb: (err: any, res: T) => void) => void, | ||
thisContext?: any, | ||
): () => Promise<T>; | ||
export default function promisify<A, T>( | ||
f: (arg: A, cb: (err: any, res: T) => void) => void, | ||
thisContext?: any, | ||
): (arg: A) => Promise<T>; | ||
export default function promisify<A, A2, T>( | ||
f: (arg: A, arg2: A2, cb: (err: any, res: T) => void) => void, | ||
thisContext?: any, | ||
): (arg: A, arg2: A2) => Promise<T>; | ||
export default function promisify<A, A2, A3, T>( | ||
f: (arg: A, arg2: A2, arg3: A3, cb: (err: any, res: T) => void) => void, | ||
thisContext?: any, | ||
): (arg: A, arg2: A2, arg3: A3) => Promise<T>; | ||
export default function promisify<A, A2, A3, A4, T>( | ||
f: (arg: A, arg2: A2, arg3: A3, arg4: A4, cb: (err: any, res: T) => void) => void, | ||
thisContext?: any, | ||
): (arg: A, arg2: A2, arg3: A3, arg4: A4) => Promise<T>; | ||
export default function promisify<A, A2, A3, A4, A5, T>( | ||
f: (arg: A, arg2: A2, arg3: A3, arg4: A4, arg5: A5, cb: (err: any, res: T) => void) => void, | ||
thisContext?: any, | ||
): (arg: A, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Promise<T>; | ||
/** | ||
* promise化 success fail 参数的api | ||
* @param func | ||
* promisify fun | ||
* @param f | ||
* @param thisContext | ||
* eg: | ||
*/ | ||
function promisify<Options extends LikeWXApi, PromisifyResult>(func: (_: Options) => void) { | ||
return (opt?: Options) => | ||
new Promise<PromisifyResult>((resolve, reject) => | ||
func({ | ||
...opt, | ||
success: resolve, | ||
fail: reject, | ||
} as Options), | ||
); | ||
export default function promisify(f: any, thisContext?: any) { | ||
return function () { | ||
// eslint-disable-next-line prefer-rest-params | ||
const args = Array.prototype.slice.call(arguments); | ||
return new Promise((resolve, reject) => { | ||
args.push((err: any, result: any) => (err ? reject(err) : resolve(result))); | ||
f.apply(thisContext, args); | ||
}); | ||
}; | ||
} | ||
|
||
export default promisify; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
import { PromiseFN } from './interfaces'; | ||
import { AnyPromiseFN } from './interfaces'; | ||
|
||
/** | ||
* 链式处理,向下传递结果 | ||
* eg:拦截器 | ||
* @param handlers | ||
*/ | ||
function sequenceChain(handlers: PromiseFN[]) { | ||
const chain = handlers.slice(); | ||
let promise = Promise.resolve(); | ||
while (chain.length) { | ||
const fulfilled = chain.shift(); | ||
const rejected = chain.shift(); | ||
promise = promise.then(fulfilled, rejected); | ||
} | ||
return promise; | ||
function sequenceChain(handlers: Array<[AnyPromiseFN, AnyPromiseFN]>) { | ||
return <T>(arg?: any): Promise<T> => { | ||
const chain = handlers.slice(); | ||
let promise = Promise.resolve(arg!); | ||
while (chain.length) { | ||
const [fulfilled, rejected] = chain.shift()!; | ||
promise = promise.then(fulfilled, rejected); | ||
} | ||
return promise; | ||
}; | ||
} | ||
|
||
export default sequenceChain; |
62a7d0e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deployment failed with the following error: