Categories
Computers

The Beaten Path

I remember when I first started Ruby; we were using JRuby in the early days of CircleCI, and also Mongo. Both of those were well supported, with many people using them. However, we seemed to be the first to ever use them together and I quickly put together this rule of thumb:

You can go off the beaten path once and be OK, but not twice.

Paul Biggar, First thoughts on Rust vs. OCaml

Also this gem:

Programming in Rust reminds me a lot of programming in C++: you add a const to one function, and then you have to follow that const around the entire codebase until you finally get to the place where you learn it actually can’t be const, and so fuck you.

Categories
Computers

Broken Promises

Let’s start here: Promise.resolve( 'hi' ).then( showHappy ).catch( showError )

This is a super common form you see with Promises, but is has a subtle problem. What happens if your then callback throws?

Arg! My catch caught the error! Because of how promises chain, this is expected, but often surprising. If your catch is written to expect a certain kind of error – maybe from a network request – it too could now fail.

If you want to be sure that the rejected promise is from the original promise, you have two options: use the second parameter to then, or write the catch first.

These methods have their own issues though. If the success handler really may throw, you may need to add a second catch to the end to handle that case.

And if you put the catch first, if that handler returns a non-Error, then following then will run:

That’s rarely what you want, but can be handy for cases where you want to transform an API error into a blank result.

Next time, how this all changes with async / await.

Categories
Computers

async / await

What do think this JS code prints?


const arr = [3, 1, 2];

const wait = (ms) => new Promise(resolve => {
  setTimeout(resolve, ms);
});

arr.forEach(async item => {
  await wait(item * 100);
  console.log(item);
})

How about this?


const arr = [3, 1, 2];

const wait = (ms) => new Promise(resolve => {
  setTimeout(resolve, ms);
});

const mapped = arr.map(async item => {
  await wait(item * 100);
  return item * 2;
})

console.log(mapped);
Categories
Computers

James Mickens on Browsers

In summary, Web browsers are like quantum physics: they offer probabilistic guarantees at best, and anyone who claims to fully understand them is a liar.

This essay is from 2014, but it’s still just as correct.