Skip to content

Instantly share code, notes, and snippets.

View adit-hotstar's full-sized avatar

Adit Shah adit-hotstar

View GitHub Profile
@adit-hotstar
adit-hotstar / promise.ts
Last active September 20, 2021 10:07
Understanding the Promises/A+ specification
type Then<A> = {
(onFulfilled?: null, onRejected?: ((reason: unknown) => A | Thenable<A>) | null): Thenable<A>;
<B>(onFulfilled: (value: A) => B | Thenable<B>, onRejected?: ((reason: unknown) => B | Thenable<B>) | null): Thenable<B>;
};
interface Thenable<A> {
then: Then<A>;
}
type PromiseState<A> =

The time when a program is executed, i.e. compile time or runtime, has nothing to do with whether that program is pure or impure.

  • You can execute pure programs at compile time.
  • You can execute pure programs at runtime.
  • You can execute impure programs at compile time.
  • You can execute impure programs at runtime.

For example, consider the following program.

@adit-hotstar
adit-hotstar / stringify.js
Created September 13, 2019 06:05
Haskell style JSON stringification.
const stringify = (json, indent = 0) => {
if (json === null || typeof json !== 'object') return JSON.stringify(json);
const indentation = `\n${' '.repeat(indent)}`;
if (Array.isArray(json)) {
const { length } = json;
if (length === 0) return '[]';