{"_id":"@baethon/promise-duck","_rev":"2-2b182223e3734851b2a7536b52ad9037","name":"@baethon/promise-duck","description":"Converts object to be promise-like","dist-tags":{"latest":"1.0.1"},"versions":{"1.0.0":{"name":"@baethon/promise-duck","version":"1.0.0","description":"Converts object to be promise-like","main":"index.js","author":{"name":"Radoslaw Mejer","email":"radmen@radmen.info"},"license":"MIT","devDependencies":{"chai":"^4.1.2","husky":"^0.14.3","mocha":"^4.1.0","standard":"^10.0.3"},"scripts":{"test":"mocha test/**/*.spec.js","lint":"standard","lint:fix":"standard --fix","prepush":"npm run lint","tag:patch":"npm version patch -m 'chore(package): bump version to %s'","tag:minor":"npm version minor -m 'chore(package): bump version to %s'","tag:major":"npm version major -m 'chore(package): bump version to %s'"},"standard":{"globals":["describe","it","before","after"]},"files":["index.js","src/**/*.js"],"gitHead":"b3d0d5dd4f497b2d79c8e6c3de8c6c4ada244826","_id":"@baethon/promise-duck@1.0.0","_npmVersion":"5.6.0","_nodeVersion":"9.2.0","_npmUser":{"name":"radmen","email":"radmen+npmjs@gmail.com"},"dist":{"integrity":"sha512-Mg1FQte3wP3Q242DLEWrSdi3ObtERbE1Zcy/nftHYROs5sHZcIPU4KmJubE1UgKNzI8Xh0XO3dZHYqBYQUpXqw==","shasum":"e6bdf6bd811aa199975e96d3db4e3c8201f5c98e","tarball":"https://registry.npmjs.org/@baethon/promise-duck/-/promise-duck-1.0.0.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIEpYynKYM+eBT4R/5Hns1/sSSFC12CC05FJj8CONBvpKAiEAjoQG3l/Z/2O8ynQn85dgUTY7l+RaU1Av+5Dl2WHL4S8="}]},"maintainers":[{"name":"radmen","email":"radmen+npmjs@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/promise-duck-1.0.0.tgz_1515517674396_0.6227385508827865"}},"1.0.1":{"name":"@baethon/promise-duck","version":"1.0.1","description":"Converts object to be promise-like","main":"index.js","author":{"name":"Radoslaw Mejer","email":"radmen@radmen.info"},"license":"MIT","devDependencies":{"chai":"^4.1.2","husky":"^0.14.3","mocha":"^4.1.0","standard":"^10.0.3"},"scripts":{"test":"mocha test/**/*.spec.js","lint":"standard","lint:fix":"standard --fix","prepush":"npm run lint","tag:patch":"npm version patch -m 'chore(package): bump version to %s'","tag:minor":"npm version minor -m 'chore(package): bump version to %s'","tag:major":"npm version major -m 'chore(package): bump version to %s'"},"standard":{"globals":["describe","it","before","after"]},"files":["index.js","src/**/*.js"],"gitHead":"823918b661b43bb696ef706b430aa511658af589","_id":"@baethon/promise-duck@1.0.1","_npmVersion":"5.6.0","_nodeVersion":"9.2.0","_npmUser":{"name":"radmen","email":"radmen+npmjs@gmail.com"},"dist":{"integrity":"sha512-TPVq2hMtBTe9PDGzr2bcNmwAxw5rSWWxNIAkeOpZglS3KHOdJKRWSEjY6a6BSEy9XBKBLw+Z/cmqop22Jw+xQg==","shasum":"6c41a2451d3a8140cb8eaecaa928a1fe4c5fcabe","tarball":"https://registry.npmjs.org/@baethon/promise-duck/-/promise-duck-1.0.1.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIEZ/x4y61ATtSDMMFTrag3FsOCpUnMLwO6b6O6A9cDjHAiAvuwL/JqfkI+ZkqPjXPAMnFPFHiAFEh+v/EP8SNiXW4A=="}]},"maintainers":[{"name":"radmen","email":"radmen+npmjs@gmail.com"},{"name":"baethon_owner","email":"radmen+npmjs.baethon@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/promise-duck-1.0.1.tgz_1515585418104_0.7977353436872363"}}},"readme":"# promise-duck\n\nConverts given callback to `Thenable` object.  \nSuch object is compatible with Promise spec so it can be used as it was a real Promise.\n\nThe difference is that computation is *deferred* until `then()` method is called or `await` keyword is used.  \nIt's quite fair to say that in the way *how* it works such Promise is quite similar to [Task](https://github.com/folktale/data.task)\n\n<!-- TOC depthFrom:2 depthTo:2 -->\n\n- [why?](#why)\n- [installation](#installation)\n- [usage](#usage)\n- [development](#development)\n\n<!-- /TOC -->\n\n## why?\n\nThe idea came from little support library which was responsible for building a query (using sort-of fluent API) which was responsible for checking if given value is unique in the database.\n\nI was wondering if object with `then()` method can be used as a regular Promise. Turned out that its possible. \n\nThis little discovery opened me a way to define friendly syntax:\n\n```js\nconst emailUnique = await isUnique.in('users').where({ email: 'jon@winterfell.com' })\n```\n\nThe defined object can be stored in a variable and none query will be executed until calling `then()` or using `await` keyword.\n\n```js\n// define details of query (no query is executed!)\nconst query = isUnique.in('users').where({ email: 'jon@winterfell.com' })\n\n// query the database and return the results\nconst emailUnique = await query\n```\n\n## installation\n\n```\nnpm i @baethon/promise-duck\n\n# or with yarn\nyarn add @baethon/promise-duck\n```\n\n## usage\n\nPackage exposes two functions:\n\n1. `thenable()` converts given function to `Thenable` object.\n1. `bind()` converts given function and binds it to the selected object\n\n### thenable()\n\n```js\nconst { thenable } = require('@baethon/promise-duck')\n\nconst heavyStuff = Math.random\nconst p = thenable(heavyStuff)\n\nconst result = await p\n```\n\n### bind()\n\n```js\nconst { bind } = require('@baethon/promise-duck')\n\nconst heavyStuff = function () {\n  return `Hello ${this.name}`\n}\n\nconst object = { name: 'Jon' }\n\nbind(heavyStuff).to(object)\n\nconst result = await object\n```\n\n### results cache\n\nBy default results generated by given function will be cached.\n\nBoth, `thenable()` and `bind()` allow to disable cache by passing `memoize: false` option:\n\n```js\nconst { thenable } = require('@baethon/promise-duck')\n\nconst fn = Math.random\nconst p = thenable(fn, { memoize: false })\n```\n\n## development\n\nIf you're planning to contribute to the package please make sure to adhere to following conventions.\n\n### tests & linting\n\n* lint your code using [standard](https://standardjs.com/); run `npm run lint` to check if there are any linting errors\n* make sure to write tests for all the changes/bug fixes\n\n### issues & PR\n\n* try to provide regression test when you find a bug\n* share some context on what you are trying to do, with enough code to reproduce the issue\n","maintainers":[{"name":"radmen","email":"radmen+npmjs@gmail.com"},{"name":"baethon_owner","email":"radmen+npmjs.baethon@gmail.com"}],"time":{"modified":"2022-04-04T17:30:11.550Z","created":"2018-01-09T17:07:55.289Z","1.0.0":"2018-01-09T17:07:55.289Z","1.0.1":"2018-01-10T11:56:58.190Z"},"author":{"name":"Radoslaw Mejer","email":"radmen@radmen.info"},"license":"MIT","readmeFilename":"README.md"}