1
votes

In the firebase docs they say that you should return a promise after a async operation

"To return data after an asynchronous operation, return a promise."

https://firebase.google.com/docs/functions/callable#sending_back_the_result

In the example they use a then to return a message to the client after the async operation finishes. But what about when I use await? Can I just return a object or do I have to wrap it inside a promise?

const response = await fetch('ttps://sandbox.itunes.apple.com/verifyReceipt', options);

if (response.status === 200)
    return {
        status: 200,
        message: "Subscription verification successfuly!"
    }

or

if (response.status === 200)
    return Promise.resolve({
        status: 200,
        message: "Subscription verification successfuly!"
    });
1

1 Answers

1
votes

Async functions return promises implicitly. So, there's no need to manually do that. This should work fine:

const fetchData = async () => {
   const response = await fetch('https://sandbox.itunes.apple.com/verifyReceipt', options);

   if (response.status === 200)
     return {
        status: 200,
        message: "Subscription verification successfuly!"
    }
   else
      return null
}

In reality, the function fetchData actually returns Promise<any> since it's marked as async