{ "_args": [ [ { "raw": "degenerator@^1.0.4", "scope": null, "escapedName": "degenerator", "name": "degenerator", "rawSpec": "^1.0.4", "spec": ">=1.0.4 <2.0.0", "type": "range" }, "/Volumes/2009-SSD/GT2/GT2-iOS/node_modules/pac-resolver" ] ], "_from": "degenerator@>=1.0.4 <2.0.0", "_id": "degenerator@1.0.4", "_inCache": true, "_location": "/degenerator", "_nodeVersion": "7.1.0", "_npmOperationalInternal": { "host": "packages-18-east.internal.npmjs.com", "tmp": "tmp/degenerator-1.0.4.tgz_1478733177407_0.3095490406267345" }, "_npmUser": { "name": "tootallnate", "email": "nathan@tootallnate.net" }, "_npmVersion": "3.10.9", "_phantomChildren": {}, "_requested": { "raw": "degenerator@^1.0.4", "scope": null, "escapedName": "degenerator", "name": "degenerator", "rawSpec": "^1.0.4", "spec": ">=1.0.4 <2.0.0", "type": "range" }, "_requiredBy": [ "/pac-resolver" ], "_resolved": "https://registry.npmjs.org/degenerator/-/degenerator-1.0.4.tgz", "_shasum": "fcf490a37ece266464d9cc431ab98c5819ced095", "_shrinkwrap": null, "_spec": "degenerator@^1.0.4", "_where": "/Volumes/2009-SSD/GT2/GT2-iOS/node_modules/pac-resolver", "author": { "name": "Nathan Rajlich", "email": "nathan@tootallnate.net", "url": "http://n8.io/" }, "bugs": { "url": "https://github.com/TooTallNate/node-degenerator/issues" }, "dependencies": { "ast-types": "0.x.x", "escodegen": "1.x.x", "esprima": "3.x.x" }, "description": "Turns sync functions into async generator functions", "devDependencies": { "mocha": "3.x.x" }, "directories": {}, "dist": { "shasum": "fcf490a37ece266464d9cc431ab98c5819ced095", "tarball": "https://registry.npmjs.org/degenerator/-/degenerator-1.0.4.tgz" }, "gitHead": "78e389ba7f058d215786b45c5152edad8ab1d062", "homepage": "https://github.com/TooTallNate/node-degenerator#readme", "license": "MIT", "main": "index.js", "maintainers": [ { "name": "tootallnate", "email": "nathan@tootallnate.net" } ], "name": "degenerator", "optionalDependencies": {}, "readme": "degenerator\n===========\n### Turns sync functions into async generator functions\n[![Build Status](https://travis-ci.org/TooTallNate/node-degenerator.svg?branch=master)](https://travis-ci.org/TooTallNate/node-degenerator)\n\nSometimes you need to write sync looking code that's really async under the hood.\nThis module takes a String to one or more synchronous JavaScript functions, and\nreturns a new String that with those JS functions transpiled into ES6 Generator\nFunctions.\n\nSo this:\n\n``` js\nfunction foo () {\n return a('bar') || b();\n}\n```\n\nGets compiled into:\n\n``` js\nfunction* foo() {\n return (yield a('bar')) || (yield b());\n}\n```\n\nFrom there, you can provide asynchronous thunk-based or Generator-based\nimplementations for the `a()` and `b()` functions, in conjunction with any\nGenerator-based flow control library to execute the contents of the\nfunction asynchronously.\n\n\nInstallation\n------------\n\nInstall with `npm`:\n\n``` bash\n$ npm install degenerator\n```\n\n\nExample\n-------\n\nYou must explicitly specify the names of the functions that should be\n\"asyncified\". So say we wanted to expose a `get(url)` function that did\nand HTTP request and returned the response body.\n\nThe user has provided us with this implementation:\n\n``` js\nfunction myFn () {\n var one = get('https://google.com');\n var two = get('http://nodejs.org');\n var three = JSON.parse(get('http://jsonip.org'));\n return [one, two, three];\n}\n```\n\nNow we can compile this into an asyncronous generator function, implement the\nasync `get()` function, and finally evaluate it into a real JavaScript function\ninstance with the `vm` module:\n\n\n``` js\nvar co = require('co');\nvar vm = require('vm');\nvar degenerator = require('degenerator');\n\n// the `get()` function is thunk-based (error handling omitted for brevity)\nfunction get (endpoint) {\n return function (fn) {\n var mod = 0 == endpoint.indexOf('https:') ? require('https') : require('http');\n var req = mod.get(endpoint);\n req.on('response', function (res) {\n var data = '';\n res.setEncoding('utf8');\n res.on('data', function (b) { data += b; });\n res.on('end', function () {\n fn(null, data);\n });\n });\n };\n}\n\n// convert the JavaScript string provided from the user (assumed to be `str` var)\nstr = degenerator(str, [ 'get' ]);\n\n// at this stage, you could use a transpiler like `facebook/regenerator`\n// here if desired.\n\n// turn the JS String into a real GeneratorFunction instance\nvar genFn = vm.runInNewContext('(' + str + ')', { get: get });\n\n// use a generator-based flow control library (`visionmedia/co`, `jmar777/suspend`,\n// etc.) to create an async function from the generator function.\n\nvar asnycFn = co(genFn);\n\n// NOW USE IT!!!\nasyncFn(function (err, res) {\n // ...\n});\n```\n\n\nAPI\n---\n\n### degenerator(String jsStr, Array functionNames) → String\n\nReturns a \"degeneratorified\" JavaScript string, with ES6 Generator\nfunctions transplanted.\n\n\nLicense\n-------\n\n(The MIT License)\n\nCopyright (c) 2013 Nathan Rajlich <nathan@tootallnate.net>\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n'Software'), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n", "readmeFilename": "README.md", "repository": { "type": "git", "url": "git://github.com/TooTallNate/node-degenerator.git" }, "scripts": { "test": "mocha --reporter spec test/test.js" }, "version": "1.0.4" }