{"_id":"@ashwynh21/completer","_rev":"6-dfb2973f60fa7b8229d43440da5487dd","time":{"created":"2022-01-16T17:33:43.803Z","1.0.0":"2022-01-14T16:39:08.472Z","modified":"2022-09-22T12:24:59.581Z","1.0.1":"2022-01-14T16:52:39.654Z","1.1.1":"2022-01-16T17:33:43.967Z","1.1.3":"2022-09-22T12:24:59.464Z"},"name":"@ashwynh21/completer","dist-tags":{"latest":"1.1.3"},"versions":{"1.1.1":{"name":"@ashwynh21/completer","version":"1.1.1","description":"A simple utility class based on the dart completer that allows de-contextualizing promises","main":"index.js","scripts":{"build":"tsc index.ts","test":"mocha test/**/*.spec.ts --recursive --exit --require ts-node/register"},"keywords":["completer","promise"],"author":{"name":"Ashwyn Horton","url":"https://github.com/ashwynh21"},"repository":{"type":"git","url":"git+https://github.com/ashwynh21/completer.git"},"license":"ISC","devDependencies":{"@types/chai":"^4.3.0","@types/mocha":"^9.0.0","@typescript-eslint/eslint-plugin":"^5.9.1","@typescript-eslint/parser":"^5.9.1","eslint":"^8.6.0","prettier":"^2.5.1","ts-node":"^10.4.0","typescript":"^4.5.4"},"dependencies":{"chai":"^4.3.4","mocha":"^9.1.4"},"gitHead":"188b15de5141d6548aa98f5fff7f3db41d19fe30","bugs":{"url":"https://github.com/ashwynh21/completer/issues"},"homepage":"https://github.com/ashwynh21/completer#readme","_id":"@ashwynh21/completer@1.1.1","_nodeVersion":"14.18.1","_npmVersion":"8.3.0","dist":{"integrity":"sha512-n5xz9/a8lmwmIaNxPOoXj1EtYArgbtXytWbs1zDJxiBsyw4YVI5TI5emEkFv4FMbEEBiG4I7gh+ywZAu65uzNw==","shasum":"2a0c14e07945ad7b5ac63e2b3a537c7dc4c671fd","tarball":"https://registry.npmjs.org/@ashwynh21/completer/-/completer-1.1.1.tgz","fileCount":17,"unpackedSize":28836,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJh5Fb3CRA9TVsSAnZWagAAV1gP/1UW38fJccgkBhTPxs4i\ngT49KB7OIBT7p/nFSBfkvMXe57hqLGlrhsfvkU6kgQXfSyn4G0dlAdmHSPeX\nNZVlZbgKeNJ+xoHgsNRctDb3E+sQrDoQN7szAKBCPZ6fawbNVKj7b7m3krZA\nYHaa6w6TIjiiiEwXM3WGjzHwps9kLCdoWuOkn52lfkf5Km1rs2BfOjOYPGT/\nMIu4t+OQ5gHu8HRnd6oNPC2sH21Xm2Oo/iOMb6wJ3vszTP+ALNRay+h0e4mK\nvG0JoOSFdcfRk+UpneCdMiUBL5esV5C355cUxtgbLKnHZrmcpDgocOwHXXh3\nrOqU9/NpBNA131fInI9FHBnLL84QhcGbnJSgSaBB/1JlvZa1UEZHDbFwPSH7\nOHT+pfoPK9rcSkNNXwAS+kVexi3vfXK8FVflW3JoHtGb84CkzwBCvFIS5Qfo\n/Pu59cUVI7HWESxP3eDf/urxwV+3b0/ZfzHMUHQxi/Ar6i2WtVIJNTQh/b79\nr7kVe8lzLC+P8seaTwwRjxJlj+2C6yfEUl7PlLHU7bFgxLjL8tRszKdhG1SE\nSp5P4RpGusrqWo+nzlNiNBwQNjz9SeEf0DQXBMB0xbIcDKoDuXibcbww0iYR\nfwJ9s1motgkW4LAdoH7/WlPk+NLpRO+a4zz/tcg732N5Vt+C4FJETVc7Rnuy\n2pXR\r\n=14HM\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDd9F7hCBSgNYvb5AbrZ1wIPqdEFT5rUHFI/ckKNViqOgIgGDI4RTOawYC9Gc30IPlRqraPjcXkAkZ+zRml8w9gOk4="}]},"_npmUser":{"name":"ashwynh21","email":"ashwynh21@gmail.com"},"directories":{},"maintainers":[{"name":"ashwynh21","email":"ashwynh21@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/completer_1.1.1_1642354423855_0.29989585096515947"},"_hasShrinkwrap":false},"1.1.3":{"name":"@ashwynh21/completer","version":"1.1.3","description":"A simple utility class based on the dart completer that allows de-contextualizing promises","main":"index.js","scripts":{"build":"tsc index.ts","test":"mocha test/**/*.spec.ts --recursive --exit --require ts-node/register"},"keywords":["completer","promise"],"author":{"name":"Ashwyn Horton","url":"https://github.com/ashwynh21"},"repository":{"type":"git","url":"git+https://github.com/ashwynh21/completer.git"},"license":"ISC","devDependencies":{"@types/chai":"^4.3.0","@types/mocha":"^9.0.0","@typescript-eslint/eslint-plugin":"^5.9.1","@typescript-eslint/parser":"^5.9.1","eslint":"^8.6.0","prettier":"^2.5.1","ts-node":"^10.4.0","typescript":"^4.5.4"},"dependencies":{"chai":"^4.3.4","mocha":"^9.1.4"},"gitHead":"147e57d3d10a584a1a569bac19a50bc6447a9823","bugs":{"url":"https://github.com/ashwynh21/completer/issues"},"homepage":"https://github.com/ashwynh21/completer#readme","_id":"@ashwynh21/completer@1.1.3","_nodeVersion":"14.20.0","_npmVersion":"6.14.17","dist":{"integrity":"sha512-9EfB74nxAPbvp8xjxU1/u5RgM++AKrBtyjoXu8k2uStf5DFE4LlNzJMFFJNdgeMpc7uKmxHiGxY2TMpAc6d2eg==","shasum":"4cfa74c6c3a542de8d7fead614cc42bf72dd1a54","tarball":"https://registry.npmjs.org/@ashwynh21/completer/-/completer-1.1.3.tgz","fileCount":10,"unpackedSize":21745,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDY3RHtr3HGmhJOmoQlpywI4VBiGqUQRyCAcElgrUPK6AIhAIQui+1y4M+O2avvSqjWdkmp1DVTAtlAtHMCxwCS98fV"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjLFQbACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmoGsA//chdxPLdNeAbLNkLCoMlbKJubv4rR3qGuboAOOsOgU/MB4oM2\r\ngsKx9LKH4mVkT7cQ+NLvs8qlTzWINLUbxohP102/IXSrgq0uS2w1OKbBfpf2\r\nUwRtwsvnG6+gkskmkRaHpVYSE4hJosmoTXuihUOzW83FcOjfPzuCgIYQhgTr\r\nRyQGmYb2O7jfdcBWntbYp70Rzb9zsSkdJPsPl8Fi610E1iUcPhv6nkCOYKYN\r\nmVihryXgrxzNVIwR5y1LBv4Qf1wP9blDbHoB8won2+s3MM4u9eLNjr+N2Ph8\r\nZm/6wnEMA4ecAF989fiC1WAfhJ8ayh+Y0lDgBj5IzUtwBLV4kjo4RUPV3qrC\r\np0sfzFjS+oQMJJEw6ckAb9K3r7qhLEVmcyv3+qrbJtH/jMIgZdgJxAisVU9F\r\nc+P0pV1Vf4U37hqvKHhNGcFpbAjA47PRYf+C3D6YP1jAIfqEe2rDuddyB4TB\r\nO6G6bJ2Y67jCOWXc0JzUzndpIDT8/Yj2gvoYB7XM71yK7Ghc+QG/10CgJFNH\r\nuhsnIBFt1arU3NLQXXzbKl83RqpSWysbHieTSrP1N5vopDKzX0q+NwB2L+VC\r\nauhnjYY/Ll7heRWhqBxpLM1cKyeVzCVT+SBciZ+WWuYNjOeha0/rog/v9IJ5\r\nL+7pNbHLv+ywO32Zx7ixUClqzqTwmXyIcDY=\r\n=o/Hz\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"ashwynh21","email":"ashwynh21@gmail.com"},"directories":{},"maintainers":[{"name":"ashwynh21","email":"ashwynh21@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/completer_1.1.3_1663849499231_0.6468781825203433"},"_hasShrinkwrap":false}},"maintainers":[{"name":"ashwynh21","email":"ashwynh21@gmail.com"}],"description":"A simple utility class based on the dart completer that allows de-contextualizing promises","homepage":"https://github.com/ashwynh21/completer#readme","keywords":["completer","promise"],"repository":{"type":"git","url":"git+https://github.com/ashwynh21/completer.git"},"author":{"name":"Ashwyn Horton","url":"https://github.com/ashwynh21"},"bugs":{"url":"https://github.com/ashwynh21/completer/issues"},"license":"ISC","readme":"# Completer\r\n\r\nA simple class that allows you to de-contextualize promises just like in ```dart```\r\n\r\n## How to install\r\n\r\n```npm install @ashwynh21/completer```\r\n\r\nIt also has ```TS``` support.\r\n\r\n## How to use\r\n\r\nThe ```Completer``` class can be used on both ```Browser``` and ```NodeJS``` since it\r\nis based on ```Promise``` instances. All it does is wrap a promise instance along with\r\na few other things that allow you to access the control properties of the promise instance\r\noutside. i.e. ```complete``` and ```reject```.\r\n\r\nHere is a set of use cases:\r\n\r\n### Browser\r\n\r\nLet's say you want to run an ```HTTP``` request to some ```API``` that will\r\ntrigger a loader and provide some data so that you can add\r\nthat data to an event listener on some button.\r\n\r\nNow, when the user clicks on this button you want to resolve\r\nsome initially running feature (loader). so that the loader\r\nonly stops when the response comes back and, the user clicks on\r\nthe button.\r\n\r\n```typescript\r\n// import the completer class\r\nimport { Completer } from '@ashwynh21/completer';\r\n\r\n// in comes the completer\r\nconst button = document.querySelector('button');\r\n// we define a completer instance\r\nconst completer = new Completer();\r\n// then get the loader\r\nconst loader = document.querySelector('#loader');\r\n\r\nconst api_call = async () => {\r\n    return new Promise((resolve, reject) => {\r\n        setTimeout(resolve, Mat.floor(Math.random() * 1E4));\r\n    })\r\n};\r\n\r\n// before calling the api request we start the loader\r\nloader.start();\r\n// then we tell the completer promise what to do when complete\r\ncompleter.then(() => loader.stop());\r\n\r\napi_call().then(() => button.addEventListener('click', () => {\r\n    // then we resolve the completer, triggering the then callback to stop the loader\r\n    completer.complete()\r\n}));\r\n```\r\n\r\n### NodeJS\r\n\r\nlet's say you have a series of ```API``` hooks, for instance, a payment set. Say a user triggers\r\na payment flow on their ```UI``` and you ```API``` triggers a request to your payment provider.\r\nYour provider will then hook the results of the payment back on another function that you have\r\ndefined.\r\n\r\nSo let us say you wanted to complete the users request, when the provider hooks back in on the\r\nsecond function you would have defined.\r\n\r\n```typescript\r\n// import the completer class\r\nimport { Completer } from '@ashwynh21/completer';\r\n\r\nconst transactions: Record<string, Completer> = {};\r\n\r\n// your initial payment hook\r\nconst payment = (id: string) => {\r\n    // make payment request to provider\r\n    transactions[id] = new Completer();\r\n  \r\n    // so by returning the completers promise, the response will only resolve once the completer is completed\r\n    return transactions[id].promise;\r\n}\r\n\r\n// the provider hook\r\nconst result = (data: { id: string }) => {\r\n    // so then here we complete the users requests since the payment has been confirmed\r\n    return transactions[data.id].complete(data);\r\n}\r\n```\r\n\r\n<hr/>\r\n\r\nI hope this tiny class does the same wonders that it has done for me.\r\n\r\n## Class Properties and Methods\r\n\r\n### Constructor\r\n\r\nThe class constructor takes an `options` object that allows you to set custom\r\nfeatures, i.e.:\r\n\r\n\r\n| Name      | Description                                                                            |\r\n| ----------- | ---------------------------------------------------------------------------------------- |\r\n| `timeout` | Allows you to set a timeout limit on the completer                                     |\r\n| `error`   | Allows you to apply a custom error to be thrown if the completer comes across an error |\r\n\r\n### Methods\r\n\r\n\r\n| Name                        | Description                                  |\r\n| ----------------------------- | ---------------------------------------------- |\r\n| `complete`                  | Called when resolving the completer promise. |\r\n| `reject`                    | Called when rejecting the completer promise. |\r\n| `then`, `catch` & `finally` | Extended from the`Promise` class             |\r\n\r\n## Contributions\r\n\r\nThis is an open source project, and its pretty simple. Please feel free to make PRs considering any\r\nideas that would make this class more sensible and well fleshed out.\r\n","readmeFilename":"README.md"}