Home > database >  Firebase Callable Cloud Function is returning void without a Promise in some cases
Firebase Callable Cloud Function is returning void without a Promise in some cases

Time:10-20

When calling a firebase callable cloud function as such:

const myFunctions = require('/path/to/functions/src/index.js');

 myFunctions.myTestFunc(data, {
       auth: {
         uid: myId
       }
     }).then(r => {
      console.log("THIS IS THE ACTUAL RESPONSE FROM CLOUD FUNCTION", r);

     })

I get: Property 'then' does not exist on type 'void | Promise'. Property 'then' does not exist on type 'void'.

Even when the callable function in question obviously is returning a resolved Promise as such:

export const myTestFunc = functions.https.onCall(
  async (data, context) => {

     return new Promise((resolve) => {
      resolve({success: true});
    });
        
  });

It still gives the warning: const myTestFunc: (req: e.Request, resp: e.Response) => void | Promise

essentially saying that in some case void is returned. However, I do not see how that is the case when I explicitly am returning a promise as above.

Any ideas about what is going on would be much appreciated.

CodePudding user response:

If you navigate to the source code for onCall(), you will see that it's defined like this:

export declare function onCall(
    handler: (data: any, context: CallableContext) => any | Promise<any>
): HttpsFunction & Runnable<any>;

You're getting an HttpsFunction, which is this:

export declare type HttpsFunction =
    TriggerAnnotated & ((req: Request, resp: Response) => void | Promise<void>);

You can see the return type of the function type is void | Promise<void>. It doesn't care what you passed to it, that's just what you get - a function that takes a Request and Response and returns a void | Promise<void>.

If you need to call that function and you want to know if it returned a promise (instead of a void) that you can call then on, then you will need to check that it returned something non-void, and it will assume Promise:

const p = myFunctions.myTestFunc(req, res);
if (p) {
    p.then(...)
}

It seems you might want to review the documentation for testing HTTP functions, if that's what you're trying to do here. There's generally no reason to call the function returned by onCall directly. There is a test library to help with that.

If you are trying to create a reusable utility function for yourself, then you shouldn't be using onCall at all. Just define and export your normal JavaScript function for any caller import to use.

  • Related