Read a file with promises in TypeScript on Node

import * as fs from “fs”; import { promisify } from “util”; const content: Promise<string> = promisify(fs.readFile)(“path/to/the/file”, { encoding: “UTF-8” }) I am tired of looking that up over and over every time I want to read a file into a promise in Node using TypeScript, so here it is. Next time, bring me here, Google. The promisify call turns fs.readFile from a callback-based method to a promise-based one. promisify accepts …

Read moreRead a file with promises in TypeScript on Node

Understanding Promises in JavaScript (with yarn and Legos)

TL;DR: creating a Promise splits off another path of execution, and the Promise object represents the end of that path. Calling .then adds code to the end of that path. You can think of your program’s execution as following a piece of yarn. this video illustrates the difference between a synchronous program and the same program …

Read moreUnderstanding Promises in JavaScript (with yarn and Legos)

Reference: Typescript asynchronous sleep that returns a promise

I want some code to execute after a delay. I want to do this with promises, in TypeScript, asynchronously. Apparently this is hard. Here is the spell: const sleepPlease: (number) => Promise<void> =     promisify( (a, b) => setTimeout(b, a)); const slow: Promise<string> =     sleepPlease(500).then(() => “yay finally”);I imported promisify from “util”. setTimeout is …

Read moreReference: Typescript asynchronous sleep that returns a promise