This repository has been archived by the owner on May 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Drop
co
, make non-Promises yieldable
- Loading branch information
1 parent
66eb9ad
commit b22b58f
Showing
4 changed files
with
84 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
interface Deferred<T> { | ||
promise: Promise<T>; | ||
resolve: (value?: T | PromiseLike<T>) => void; | ||
reject: (reason?: any) => void; | ||
} | ||
|
||
function isGen(fn: any): fn is GeneratorFunction { | ||
return ( | ||
typeof fn == 'function' && fn.constructor.name == 'GeneratorFunction' | ||
); | ||
} | ||
|
||
function createDeferred<T>(): Deferred<T> { | ||
let r; | ||
let j; | ||
const promise = new Promise<T>( | ||
( | ||
resolve: (value?: T | PromiseLike<T>) => void, | ||
reject: (reason?: any) => void | ||
): void => { | ||
r = resolve; | ||
j = reject; | ||
} | ||
); | ||
if (!r || !j) { | ||
throw new Error('Creating Deferred failed'); | ||
} | ||
return { promise, resolve: r, reject: j }; | ||
} | ||
|
||
export default function generatorToPromise<T>( | ||
generatorFunction: any | ||
): (...args: any[]) => Promise<T> { | ||
if (!isGen(generatorFunction)) { | ||
throw new Error('The given function must be a generator function'); | ||
} | ||
|
||
return function invoker(this: any) { | ||
const deferred = createDeferred<T>(); | ||
const generator = generatorFunction.call(this, arguments); | ||
(function next(error?: Error | null, value?: any) { | ||
let genState = null; | ||
try { | ||
if (error) genState = generator.throw(error); | ||
else genState = generator.next(value); | ||
} catch (e) { | ||
genState = { value: Promise.reject(e), done: true }; | ||
} | ||
|
||
if (genState.done) { | ||
deferred.resolve(genState.value); | ||
} else { | ||
Promise.resolve(genState.value) | ||
.then(promiseResult => next(null, promiseResult)) | ||
.catch(error => next(error)); | ||
} | ||
})(); | ||
|
||
return deferred.promise; | ||
}; | ||
} |
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