Based on David's Flanagan implementation. It adds a write API and uses promises.