Skip to content

Instantly share code, notes, and snippets.

@MrHamidKhan
Created July 1, 2023 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrHamidKhan/97f36fed36f7450a39651d297f9b9b4b to your computer and use it in GitHub Desktop.
Save MrHamidKhan/97f36fed36f7450a39651d297f9b9b4b to your computer and use it in GitHub Desktop.
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);
  });

  return {
    promise,
    resolver,
    rejector
  }
};
Associated Context
Type Code Snippet ( .ts )
Associated Tags higher-order-components angular Rejection Handling Promise constructor union-types Reject method Rejector function Promise Resolver PromiseResolution function JavaScript Frameworks Generic types Promise Resolution union-types typescript-generics higher-order-components Promise object Asynchronous programming Error handling Error Handling angular typescript-typings flowtype flowtype Promise Rejector Resolver function Resolve method Functional Programming TypeScript SDK typescript-generics Framework:TypeScript Asynchronous Programming Object-Oriented Programming typescript-typings
💡 Smart Description 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.
🔎 Suggested Searches Typescript Promise Resolution
Promise Resolver and Rejector
Promise with Typescript
Typescript Promise Utility
Typescript Promise Helper
React PromiseResolution resolver and rejectors
How to create a promise with resolvers in React?
Using resolve() function eact for resolving or rejected promises
Creating an instance of Promiseseact using Resolver/Rejector methods
Related Links https://www.digitalocean.com/community/tutorials/understanding-javascript-promises
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
https://javascript.info/promise-basics
https://www.freecodecamp.org/news/javascript-promises-explained/
https://www.geeksforgeeks.org/javascript-promise/
https://www.w3schools.com/js/js_promise.asp
Related People Hamid Khan, Hamid Khan
Sensitive Information No Sensitive Information Detected
Shareable Link https://user-51b68b9a-00ae-425b-b493-f47f99125377-tcopy4l54a-ue.a.run.app/?p=9937499e8f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment