Skip to content

Instantly share code, notes, and snippets.

@MrHamidKhan
MrHamidKhan / Promise Resolution Generator.md
Created July 1, 2023 13:29
This code exports a function that returns an object containing a promise, a resolver, and a rejector, all of which are related to a generic type T. This code defines a PromiseResolution function that returns an object with two resolvers and rejectors, which can be used to resolve or reject the promise.

Promise Resolution Generator

Preview:
export const PromiseResolution: { <T>(): { resolver: { (args: T): T | void }; rejector: { (args: T): T | void }; promise: Promise<T> } } = <T>() => {
  let resolver!: { (args: T): T | void };
  let rejector!: { (args: T): T | void };

  const promise: Promise<T> = new Promise<T>((resolve: { (args: T): T | void }) => {
    resolver = (args: T) => resolve(args);
    rejector = (args: T) => resolve(args);