Java port of when.js