JennyToy
JennyToy

Reputation: 628

Generic promise

I currently am managing code that has this function

public getData(somei: Somei): Promise<SomeD> {

       return new Promise((resolve, reject) => {
               this.getSomeD(somei).subscribe((someData: SomeD) => {
                   resolve(someData);
               });
        });

    }

This works fine. I would like to add a then to perform an action after the code above executes but I dont seem to get it to work. I tried it like

public getData(somei: Somei): Promise<SomeD> {

       return new Promise((resolve, reject) => {
               this.getSomeD(somei).subscribe((someData: SomeD) => {
                   resolve(someData);
               });
        }).then(()=>{callanotherFunction()});

    }

but I get the typescript error

 Type 'Promise<void>' is not assignable to type 'Promise<SomeD>'.

I imagine I have to return the right object type? I should say I am not that familiar with promises.

Upvotes: 2

Views: 1268

Answers (3)

Mark
Mark

Reputation: 92440

It's complaining because your second promise (the then()) the is not returning anything, but your signature says it will. If callanotherFunction returns something you can return that by removing the braces in the then body:

then(()=>callanotherFunction());

or explicitly return something:

.then(()=>{
   return callanotherFunction()
});

If callanotherFunction() doesn't return anything, then your signature is wrong and should be changed to reflect this:

public getData(somei: Somei): Promise<Void> {

You can also pass the value from the first promise into then and return it making callanotherFunction() just a side-effect:

 .then((someData)=>{
   callanotherFunction()
   return someData
});

This assumes that callanotherFunction() does not perform some async operation that needs to happen first.

Better than all of this is probably to take @jb-nizet's advice in the comments and use the observables directly.

Upvotes: 2

Thiago Martins
Thiago Martins

Reputation: 119

Specify the promise type to be returned

return new Promise<SomeD>(/* blablabla */);

Upvotes: -1

CRice
CRice

Reputation: 32176

The line

.then(()=>{callanotherFunction()});

Is the problem. Since that anonymous function does not return anything (it only invokes callanotherFunction), your promise has changed from returning a SomeD to returning void, which is the error typescript is pointing out to you.

As you say, the solution is to return the right thing. In this case, I think you have two options. Either save the promise to a variable, do your .then on that, then return the original. Or you can intercept the result of the original and just forward it in your anonymous function.

Here's an example of both:

const prom = new Promise((resolve, reject) => {
    this.getSomeD(somei).subscribe((someData: SomeD) => {
        resolve(someData);
    });
});

// Do your code when promise resolves, but return original:
prom.then(() => { callanotherFunction() });
return prom;

Or, to just forward the result:

return new Promise((resolve, reject) => {
    this.getSomeD(somei).subscribe((someData: SomeD) => {
        resolve(someData);
    });
}).then(result => { callanotherFunction(); return result; }); // Forward the result

Using either of those, the return type will remain as Promise<SomeD>.

Upvotes: 4

Related Questions