{"_id":"@assertchris/attempt-promise","_rev":"1-174df16bb47f00408cf6e746164f32e0","name":"@assertchris/attempt-promise","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@assertchris/attempt-promise","version":"1.0.0","description":"Alternative to try-catch wrapping async/await","main":"index.js","scripts":{"test":"node test.js"},"homepage":"https://github.com/assertchris/attempt-promise#readme","repository":{"type":"git","url":"git+https://github.com/assertchris/attempt-promise.git"},"bugs":{"url":"https://github.com/assertchris/attempt-promise/issues"},"author":{"name":"Christopher Pitt","email":"cgpitt@gmail.com","url":"https://assertchris.io"},"license":"MIT","gitHead":"ae3d34af497b27dc5f986fe5d1a7c564c884256e","_id":"@assertchris/attempt-promise@1.0.0","_npmVersion":"6.4.1","_nodeVersion":"11.2.0","_npmUser":{"name":"assertchris","email":"cgpitt@gmail.com"},"dist":{"integrity":"sha512-r4RBXVTiPVTGcklvRWhEiGkso2OohqbWvlKHCg9a+Brm6I3ytFuhoT/8um1OIuFsjMg5JZ3dmjqm6tqbbIURdA==","shasum":"46717118e6f23dc140efa1a19597069e61fef735","tarball":"https://registry.npmjs.org/@assertchris/attempt-promise/-/attempt-promise-1.0.0.tgz","fileCount":6,"unpackedSize":7655,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJb9YkHCRA9TVsSAnZWagAAjxkP/i1ABJWSSWPw7gASW+7a\ne4keiu3R9Wb3B/DmgVF+avdOlfWBnciOtzk6t/oYjwN7Z4OPxv7/X97m+bRx\nWVWb5KMOfLItg7SQMpg1R5Y6XB8eXmqvczaPZbKw/OhxBVqpK5ruJmlJaZeD\nv73XCx79wE6Rf7sd+kKHxm1mspJYbudjl4swNxz1e82d+IWqAcxfoR7tPHEi\n/ltlQpFMW+NkHyxd6oIsecD45n3UsPbCtFyj/6F+9BcqWHjTWROkempXFAJc\nr+7bStN7dzmwvio21+uqEH7cC1OLhotYvG1Mc0FgfJWCDza2XpnPMB8Ibs3t\nhithK2at9AWsdZQF5yECku21IyyZfM1Dktoh2sM37pZhz74crFtA6BnUUiUV\nYz5uLxYxJV+5clfqMEweAV0TpWLaalD/iHcpLFb7xLh0KBqmi34FSa3rEnsv\nZwVDkG1ctppeMMdsMCtXd/YPUKD+pRDY/YNxT7goah/sm1mVjmJvTsVlAtw+\nrTiisdRq/+kv/eD6CDPwL74buQafVDPWgyEIxanMKIwuYC0b+mIOFIwFMdsr\n2Js2G/kkV+IVr6GGc7Xtg/MPRiHvy4G07ymqbhnvtnZqoR/iycde/NreqwEL\nXAdvgI+vJctkxFWDsKImNmY0446reCwVxGx/L4Or1SDgMA/Uhbz3Lrofzh7K\n4R26\r\n=zZG3\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCID2SOrPGHuXcVguI199qBCkaMwL9h1v4UGS0yQs8/C0mAiBD+IcRqKnPMgqrQn8+aWqdCzBQ9Lw6VVJNofl8rMlN2Q=="}]},"maintainers":[{"name":"assertchris","email":"cgpitt@gmail.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/attempt-promise_1.0.0_1542818055200_0.2963687388095564"},"_hasShrinkwrap":false}},"time":{"created":"2018-11-21T16:34:15.025Z","1.0.0":"2018-11-21T16:34:15.426Z","modified":"2022-04-04T15:50:59.148Z"},"maintainers":[{"name":"assertchris","email":"cgpitt@gmail.com"}],"description":"Alternative to try-catch wrapping async/await","homepage":"https://github.com/assertchris/attempt-promise#readme","repository":{"type":"git","url":"git+https://github.com/assertchris/attempt-promise.git"},"author":{"name":"Christopher Pitt","email":"cgpitt@gmail.com","url":"https://assertchris.io"},"bugs":{"url":"https://github.com/assertchris/attempt-promise/issues"},"license":"MIT","readme":"`async` and `await` are great! SO much better than callbacks all over the place, and helpful for showing that functions are asynchronous, without having to read the function.\n\nWhat's not so great is having to wrap calls to `async` functions in a try-catch.\n\nMaybe this code looks like something in your application:\n\n```js\nconst id = session.get(\"id\");\n\nlet user = undefined;\nlet products = undefined;\n\ntry {\n  user = await User.find(id);\n\n  try {\n    products = await Product.where(\"user_id\", user.id)\n      .orderBy(\"updated_at\", \"desc\")\n      .limit(50)\n      .fetch();\n  } catch (e) {\n    response.error(\"Oops! Missing products...\");\n    return;\n  }\n} catch (e) {\n  response.error(\"Oops! No user...\");\n  return;\n}\n\nresponse.ok(\"here are your products...\", products);\n```\n\nThis is ok, but there are a couple things I don't like about it:\n\n1. I have to pre-define variables, or they're hidden in the scope of the try-catches\n2. The error handling, for the missing user, is far away from the attempt to fetch the user\n3. There are multiple levels of nesting, for what is supposed to be a linear process\n\nI feel like the gains of being able to avoid promise callbacks are lost by having to wrap everything in a try-catch. I was inspired by some syntax from Go:\n\n```go\nuser, err := User.find(id)\n\nif err != nil {\n    log.Fatal(err)\n}\n\n// do something with user\n```\n\nSo, I implemented something similar, here. Check out how the first example changes, given this new syntax:\n\n```js\nconst attempt = require(\"@assertchris/attempt-promise\");\n\nconst id = session.get(\"id\");\n\nconst [err1, user] = await attempt(User.find(id));\n\nif (err1) {\n  response.error(\"Oops! No user...\");\n  return;\n}\n\nconst [err2, products] = await attempt(\n  Product.where(\"user_id\", user.id)\n    .orderBy(\"updated_at\", \"desc\")\n    .limit(50)\n    .fetch(),\n);\n\nif (err2) {\n  response.error(\"Oops! Missing products...\");\n  return;\n}\n\nresponse.ok(\"here are your products...\", products);\n```\n\nUse one of these to install:\n\n```\nnpm install @assertchris/attempt-promise\nyarn add @assertchris/attempt-promise\n```\n","readmeFilename":"readme.md"}