How to really use ES6 promises

May 12th, 2016, 07:02am UTC by Matt Miller in Node.js

ES6 promises are powerful alternatives to the callback-style approach of asynchronous JavaScript behavior, but some of the lesser-documented features of promises, particularly around their chainability, are what make them most appealing.

At its simplest, here is how callback-style coding is replaced with promises in ES6:

// Error-first callback style
function getUser (userId, done) {
  // Make a db call to fetch the user, then...
  err ? done(err) : done(null, user);
}
getUser(42, (err, user) => {
  if (err) { throw err; }
  // do stuff with user...
});

// Promise style
function getUser (userId) {
  return new Promise((resolve, reject) => {
    // Make a db call to fetch the user, then...
    err ? reject(err) : resolve(user);
  });
}
getUser(42)
  .then(user => { /* do stuff with user... */ })
  .catch(err => { throw err; });

This approach is not very practical -- usually, you would not invoke the Promise constructor directly. Also, this approach doesn't offer…

Validation: Comprehensive or friendly?

February 6th, 2016, 10:02pm UTC by Matt Miller

I'm in the preliminary planning stages of an API framework and have started to tackle the issue of validation of input.

When you're hacking together your first API, you might throw some logic into a controller for your basic validation: if (typeof req.body.name !== 'string' || !req.body.name.length) res.send('Missing name!'). Of course, this approach doesn't scale as your data sets get larger and contain convoluted, nested structures. Plus, it mucks up your controllers.

In today's world of complex object models, clearly the best solution to validation is JSON schemas. JSON schemas are JSON documents themselves and they define property types, enums, range restrictions, pattern matching, and a ton more. Most importantly, it's trivial to define a schema that inherits from another schema, or reuse specific schemas by embedding them in others using references. JSON schemas are a powerful and unmatched system for validating documents of just about any…

ZenHub is awesome

January 18th, 2016, 11:59pm UTC by Matt Miller

From their website:

Organize and visualize entire projects without leaving GitHub: ZenHub’s Task Boards are your team’s project management hub. Boards make organization-wide prioritization simple by connecting several repos in one place, yet adapt to any workflow with powerful filters and a customizable layout.

ZenHub is a kanban board that is integrated directly in GitHub. It simply uses the existing GitHub issue tracker and generates a nice UI on top of it via a Chrome extension. It's beautiful -- it keeps GitHub as your definitive source for code, pull requests, and issues, rather than splitting your knowledgebase between your repository and some other project management software. It's lightweight but it has most of what you need to manage a software project, integrated directly in a place you already visit.

I'm now using ZenHub for my new project, Autonym, which is a pragmatic API…