Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

typescript promise

new Promise<boolean>((res, rej) => {
  res(true);
})
.then(res => {
  console.log(res);
  return false;
})
  .then(res => {
  console.log(res);
  return true;
})
  .then(res => {
  console.log(res);
})
  .catch(error => {
  console.log('ERROR:', error.message);
});
Comment

typescript get the promise return type

type AsyncReturnType<T extends (...args: any) => any> =
	T extends (...args: any) => Promise<infer U> ? U :
	T extends (...args: any) => infer U ? U :
	any
Comment

typescript get type from promise

function promiseOne() {
  return Promise.resolve(1)
}
    
const promisedOne = promiseOne()
    
// note PromiseLike instead of Promise, this lets it work on any thenable
type ThenArg<T> = T extends PromiseLike<infer U> ? U : T
    
type PromiseOneThenArg = ThenArg<typeof promisedOne> // => number
// or
type PromiseOneThenArg2 = ThenArg<ReturnType<typeof promiseOne>> // => number
Comment

Get Promise type TypeScript

//return from promise
type promiseReturn<T extends Promise<unknown>> = T extends Promise<infer type>
	? type extends Promise<unknown>
		? promiseReturn<type>
		: type
	: never;
type prom = promiseReturn<Promise<string>>; //string
type prom_2 = promiseReturn<Promise<Promise<string>>>; //string
Comment

PREVIOUS NEXT
Code Example
Typescript :: react update state array of objects hooks 
Typescript :: styled components tw 
Typescript :: typescript parse to string 
Typescript :: real time charts in flutter 
Typescript :: distance between two lat long points google maps api 
Typescript :: returning objects in alphabetical order in ruby 
Typescript :: laws of ux: using psychology to design better products & services pdf 
Typescript :: you can initiate objects from a 
Typescript :: vb net code snippets for storing password 
Typescript :: UpdateTable operation with the GlobalSecondaryIndexUpdates parameter 
Cpp :: hello world c++ 
Cpp :: how to list environments with conda 
Cpp :: c++ vector decimal to binary 
Cpp :: how to print list in c++ 
Cpp :: c++ hello word 
Cpp :: how to output text in c++ 
Cpp :: clear file before writing c++ 
Cpp :: C++ red text output 
Cpp :: c++ allocate and free dynamic 2d array 
Cpp :: binary search return index c++ 
Cpp :: qimage transformed 
Cpp :: map of vector of struct error 
Cpp :: how to set a string equal to another string cpp 
Cpp :: How to block window resize sfml c++ 
Cpp :: differency between c++ std and stl 
Cpp :: ifstream relative file path 
Cpp :: n queens c++ 
Cpp :: how to print with the bool value in cpp 
Cpp :: c++ print to standard error 
Cpp :: c++ read integers from file 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =