Giter Site home page Giter Site logo

attempt-promise's Introduction

attempt-promise

support

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.

What's not so great is having to wrap calls to async functions in a try-catch.

Maybe this code looks like something in your application:

const id = session.get("id");

let user = undefined;
let products = undefined;

try {
  user = await User.find(id);

  try {
    products = await Product.where("user_id", user.id)
      .orderBy("updated_at", "desc")
      .limit(50)
      .fetch();
  } catch (e) {
    response.error("Oops! Missing products...");
    return;
  }
} catch (e) {
  response.error("Oops! No user...");
  return;
}

response.ok("here are your products...", products);

This is ok, but there are a couple things I don't like about it:

  1. I have to pre-define variables, or they're hidden in the scope of the try-catches
  2. The error handling, for the missing user, is far away from the attempt to fetch the user
  3. There are multiple levels of nesting, for what is supposed to be a linear process

I 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:

user, err := User.find(id)

if err != nil {
    log.Fatal(err)
}

// do something with user

So, I implemented something similar, here. Check out how the first example changes, given this new syntax:

const attempt = require("@assertchris/attempt-promise");

const id = session.get("id");

const [err1, user] = await attempt(User.find(id));

if (err1) {
  response.error("Oops! No user...");
  return;
}

const [err2, products] = await attempt(
  Product.where("user_id", user.id)
    .orderBy("updated_at", "desc")
    .limit(50)
    .fetch()
);

if (err2) {
  response.error("Oops! Missing products...");
  return;
}

response.ok("here are your products...", products);

Installing

Use one of these to install:

npm install @assertchris/attempt-promise
yarn add @assertchris/attempt-promise

attempt-promise's People

Contributors

assertchris avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

leechael

attempt-promise's Issues

some feedback / questions

I like your way of thinking with this quite a lot (it's also nice to see some "multiple returns" with array destruction in the wild!)

but I do feel like, and please correct me if I'm wrong that the try-catch problem could be solved with vanilla JS.
This is because, and again please correct me if wrong, a promise is like a try block in and of itself, with optional catch blocks in the form of a call to the catch method or a second callback for then

to illustrate I take your example and transform it to vanilla JS

const id = session.get("id");

const [err1, user] = await attempt(User.find(id));

if (err1) {
  response.error("Oops! No user...");
  return;
}

const [err2, products] = await attempt(
  Product.where("user_id", user.id)
    .orderBy("updated_at", "desc")
    .limit(50)
    .fetch(),
);

if (err2) {
  response.error("Oops! Missing products...");
  return;
}

response.ok("here are your products...", products);

vanilla:

const id = session.get("id");

return User.find(id)
    .catch(e => response.error("Oops! No user..."))
    .then(user => Product.where("user_id", user.id).orderBy("updated_at", "desc").limit(50).fetch())
    .catch(e => response.error("Oops! Missing products..."))
    .then(products = > response.ok("here are your products...", products));

(I would like to note that I have some doubts about the vanilla one regarding the catch calls, in my expierence, but its been a while, the catch does not break the chain. However the information I just looked up(https://stackoverflow.com/a/20715224) suggest it does, so I wrote the code based on what I found);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.