rewrite in TypeScript with Q