{"_id":"then-yield","_rev":"9-8fc352f7f24d2eebb88ee3c991a46afc","name":"then-yield","description":"Promise based generators with no dependency on a specific promise lib","dist-tags":{"latest":"0.0.1"},"versions":{"0.0.1":{"name":"then-yield","version":"0.0.1","description":"Promise based generators with no dependency on a specific promise lib","keywords":[],"dependencies":{},"devDependencies":{"promise":"~3.2.0","gnode":"0.0.6"},"scripts":{"test":"gnode test/index.js"},"repository":{"type":"git","url":"https://github.com/then/yield.git"},"author":{"name":"ForbesLindesay"},"license":"MIT","bugs":{"url":"https://github.com/then/yield/issues"},"homepage":"https://github.com/then/yield","_id":"then-yield@0.0.1","dist":{"shasum":"04da3ba43e2428a5ff8fde7b549155c3e5b0c57c","tarball":"https://registry.npmjs.org/then-yield/-/then-yield-0.0.1.tgz","integrity":"sha512-SGZy+++MsA3uV1kDKjzKJop/ZBoMrAv+ekyydUJMT4zRgvJ0YkXTkMlo9iynJycpnXy2UoM7qtOpW18LifabSQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCfkFu0//CGUnc2KJ079eQgX4RpeYv8hBBdkGk7udgQqgIgXX3p+Sp3emx+RioNqLvn7PkUXvzCMMt+7fma7IT0vTY="}]},"_from":".","_npmVersion":"1.3.14","_npmUser":{"name":"forbeslindesay","email":"forbes@lindeay.co.uk"},"maintainers":[{"name":"forbeslindesay","email":"forbes@lindeay.co.uk"}]}},"readme":"# then-yield\n\nPromise based generators with no dependency on a specific promise library.\n\nTo create a version specific to your library, just use:\n\n```javascript\nvar ty = require('then-yield').using(Promise.cast);\n```\n\nThe goal is to be performant and to ensure that it is as versatile as possible while maintaining a simple interface.  You can use `yield` to wait for a `Promise`, an `Array.<Promise>`.  It also allows you to `yield` a `Generator` (the result of calling a `GeneratorFunction`) but it is preferable to wrap each generator function in `async` or use `yield*`.\n\n[![Build Status](https://travis-ci.org/then/yield.png?branch=master)](https://travis-ci.org/then/yield)\n[![Dependency Status](https://gemnasium.com/then/yield.png)](https://gemnasium.com/then/yield)\n[![NPM version](https://badge.fury.io/js/then-yield.png)](http://badge.fury.io/js/then-yield)\n\n## Installation\n\n    npm install then-yield\n\n## Usage\n\n### spawn(fn, unwrap)\n\nImmediately evaluate an asynchronous generator function.\n\n```js\nvar result = ty.spawn(function* () {\n  var src = yield readFilePromise('foo.json', 'utf8');\n  return JSON.parse(src);\n});\n```\n\nYou may optionally cast the result to a promise.\n\n```js\nvar result = ty.spawn(function* () {\n  var src = yield readFilePromise('foo.json', 'utf8');\n  return JSON.parse(src);\n}, Promise.cast);\n```\n\nThis also handles any mis-behaving promises/thenables by calling `Promise.cast`.\n\nFinally, you can insert a delay for each yield:\n\n```js\nvar result = ty.spawn(function* () {\n  var src = yield readFilePromise('foo.json', 'utf8');\n  return JSON.parse(src);\n}, Promise.cast, function (value) {\n  return new Promise(resolve => {\n    setTimeout(() => resolve(value), 100);\n  });\n});\n```\n\n### async(fn, unwrap)\n\nBind an asynchronous generator function to be used later.\n\n```js\nvar readJSON = ty.async(function* (filename) {\n  var src = yield readFilePromise(filename, 'utf8');\n  return JSON.parse(src);\n});\n```\n\nYou may optionally cast the result to a promise.\n\n```js\nvar readJSON = ty.async(function* (filename) {\n  var src = yield readFilePromise(filename, 'utf8');\n  return JSON.parse(src);\n}, Promise.cast);\n```\n\nThis also handles any mis-behaving promises/thenables by calling `Promise.cast`.\n\nFinally, you can insert a delay for each yield:\n\n```js\nvar readJSON = ty.async(function* (filename) {\n  var src = yield readFilePromise(filename, 'utf8');\n  return JSON.parse(src);\n}, Promise.cast, function (value) {\n  return new Promise(resolve => {\n    setTimeout(() => resolve(value), 100);\n  });\n});\n```\n\n### using(castPromise[, unwrapStep])\n\nBy default, generators return values, rather than promises, if they never yield a promise.  They also just return a promise of the type of the first promise to be yielded.  This randomness is not always what you want, so we provide the `using` method that allows you to override this behavior:\n\n```js\nvar ty = require('ty').using(Promise.cast);\n```\n\nBy default, this is only applied to the final result, and any intermediate promises.  This helps improve performance, but might not always be desirable.  The second argument allows you to unwrap any value.  This could be used to insert delays, or perhaps to support deep-resolution of objects:\n\n```js\nvar readFiles = ty.using(Promise.cast, unwrap).async(function* () {\n  let {left, right} = yield {left: read('left'), right: read('right')};\n  return left + right;\n});\n\nfunction unwrap(obj) {\n  if (Array.isArray(obj)) return Promise.all(obj);\n  if (obj && typeof obj === 'object') {\n    var keys = Object.keys(obj);\n    var values = Promise.all(keys.map(function (key) { return unwrap(obj[key]); }));\n    return values.then(function (values) {\n      for (var i = 0; i < values.length; i++) {\n        obj[keys[i]] = values[i];\n      }\n      return obj;\n    });\n  }\n  return obj;\n}\n```\n\n## License\n\n  MIT","maintainers":[{"name":"forbeslindesay","email":"forbes@lindeay.co.uk"}],"time":{"modified":"2022-06-27T06:03:34.779Z","created":"2014-02-11T11:25:32.627Z","0.0.1":"2014-02-11T11:25:33.986Z"},"readmeFilename":"README.md","author":{"name":"ForbesLindesay"},"repository":{"type":"git","url":"https://github.com/then/yield.git"},"users":{"cage1618":true}}