Home > Blockchain >  Change promise type
Change promise type

Time:07-14

I have a function that should return Promise<Type1> and the result is actually obtained by calling another function which returns Promise<Type2>. How should I do that? Is the following correct? Thanks,

function1():Promise<Type1>{
  return function2().then(result => {...some code that convert the result (Type2) to a Type1 object...});
}

CodePudding user response:

If you want to map from one to another type, you can do it inside the .then() callback, as you already did.

But don't forget to change the return type of function1 when your actual return type should be Type2:

function1(): Promise<Type2>{
  return function2.then(result => {...some code that convert the result (Type1) to a Type2 object...});
}
  • Related