degenerator
Advanced tools
Comparing version 2.1.4 to 2.2.0
@@ -1,1 +0,1 @@ | ||
export default function generatorToPromise<T>(generatorFunction: any): (...args: any[]) => Promise<T>; | ||
export default function generatorFnToPromise<T>(generatorFunction: any): (...args: any[]) => Promise<T>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function isGen(fn) { | ||
function isGenerator(fn) { | ||
return fn && fn.next && fn.throw; | ||
} | ||
function isGeneratorFunction(fn) { | ||
return (typeof fn == 'function' && fn.constructor.name == 'GeneratorFunction'); | ||
@@ -18,7 +21,9 @@ } | ||
} | ||
function generatorToPromise(generatorFunction) { | ||
if (!isGen(generatorFunction)) { | ||
function generatorFnToPromise(generatorFunction) { | ||
if (!isGeneratorFunction(generatorFunction)) { | ||
if (typeof generatorFunction === 'function') { | ||
return function (...args) { | ||
return Promise.resolve(true).then(() => generatorFunction.apply(this, args)); | ||
return Promise.resolve(true).then(() => { | ||
return generatorFunction.apply(this, args); | ||
}); | ||
}; | ||
@@ -29,28 +34,36 @@ } | ||
return function (...args) { | ||
const deferred = createDeferred(); | ||
const generator = generatorFunction.apply(this, args); | ||
(function next(error, value) { | ||
let genState = null; | ||
try { | ||
if (error) | ||
genState = generator.throw(error); | ||
else | ||
genState = generator.next(value); | ||
return generatorToPromise(generator); | ||
}; | ||
} | ||
exports.default = generatorFnToPromise; | ||
function generatorToPromise(generator) { | ||
const deferred = createDeferred(); | ||
(function next(err, value) { | ||
let genState = null; | ||
try { | ||
if (err) { | ||
genState = generator.throw(err); | ||
} | ||
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)); | ||
genState = generator.next(value); | ||
} | ||
})(); | ||
return deferred.promise; | ||
}; | ||
} | ||
catch (e) { | ||
genState = { value: Promise.reject(e), done: true }; | ||
} | ||
if (isGenerator(genState.value)) { | ||
genState.value = generatorToPromise(genState.value); | ||
} | ||
if (genState.done) { | ||
deferred.resolve(genState.value); | ||
} | ||
else { | ||
Promise.resolve(genState.value) | ||
.then(promiseResult => next(null, promiseResult)) | ||
.catch(err => next(err)); | ||
} | ||
})(); | ||
return deferred.promise; | ||
} | ||
exports.default = generatorToPromise; | ||
//# sourceMappingURL=generator-to-promise.js.map |
/// <reference types="node" /> | ||
import { Context, RunningScriptOptions } from 'vm'; | ||
/** | ||
* Turns sync JavaScript code into an JavaScript with async Functions. | ||
* Compiles sync JavaScript code into JavaScript with async Functions. | ||
* | ||
* @param {String} code JavaScript string to convert | ||
* @param {Array} names Array of function names to add `yield` operators to | ||
* @param {Array} names Array of function names to add `await` operators to | ||
* @return {String} Converted JavaScript string with async/await injected | ||
@@ -15,10 +15,12 @@ * @api public | ||
type DegeneratorNames = DegeneratorName[]; | ||
type DegeneratorOutput = 'async' | 'generator'; | ||
interface DegeneratorOptions { | ||
output?: string; | ||
output?: DegeneratorOutput; | ||
} | ||
type CompileOptions = DegeneratorOptions & RunningScriptOptions & { | ||
interface CompileOptions extends DegeneratorOptions, RunningScriptOptions { | ||
sandbox?: Context; | ||
}; | ||
function compile<T>(code: string, returnName: string, names: DegeneratorNames, options?: CompileOptions): T; | ||
} | ||
const supportsAsync: boolean; | ||
function compile<T extends Function>(code: string, returnName: string, names: DegeneratorNames, options?: CompileOptions): T; | ||
} | ||
export = degenerator; |
@@ -13,6 +13,6 @@ "use strict"; | ||
/** | ||
* Turns sync JavaScript code into an JavaScript with async Functions. | ||
* Compiles sync JavaScript code into JavaScript with async Functions. | ||
* | ||
* @param {String} code JavaScript string to convert | ||
* @param {Array} names Array of function names to add `yield` operators to | ||
* @param {Array} names Array of function names to add `await` operators to | ||
* @return {String} Converted JavaScript string with async/await injected | ||
@@ -124,2 +124,3 @@ * @api public | ||
(function (degenerator) { | ||
degenerator.supportsAsync = supports_async_1.default; | ||
function compile(code, returnName, names, options = {}) { | ||
@@ -136,3 +137,8 @@ const output = supports_async_1.default ? 'async' : 'generator'; | ||
else { | ||
return generator_to_promise_1.default(fn); | ||
const rtn = generator_to_promise_1.default(fn); | ||
Object.defineProperty(rtn, 'toString', { | ||
value: fn.toString.bind(fn), | ||
enumerable: false | ||
}); | ||
return rtn; | ||
} | ||
@@ -139,0 +145,0 @@ } |
{ | ||
"name": "degenerator", | ||
"version": "2.1.4", | ||
"description": "Turns sync functions into async generator functions", | ||
"version": "2.2.0", | ||
"description": "Compiles sync functions into async generator functions", | ||
"main": "dist/src/index", | ||
@@ -36,3 +36,3 @@ "typings": "dist/src/index", | ||
"@types/mocha": "^5.2.7", | ||
"@types/node": "^10.5.3", | ||
"@types/node": "^12.12.17", | ||
"@typescript-eslint/eslint-plugin": "1.6.0", | ||
@@ -50,4 +50,4 @@ "@typescript-eslint/parser": "1.1.0", | ||
"rimraf": "^3.0.0", | ||
"typescript": "^3.5.3" | ||
"typescript": "^3.7.3" | ||
} | ||
} |
degenerator | ||
=========== | ||
### Turns sync functions into async functions | ||
### Compiles sync functions into async functions | ||
[![Build Status](https://github.com/TooTallNate/node-degenerator/workflows/Node%20CI/badge.svg)](https://github.com/TooTallNate/node-degenerator/actions?workflow=Node+CI) | ||
@@ -5,0 +5,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
25272
306