Reputation: 3489
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!"
});
Upvotes: 1
Views: 41
Reputation: 2544
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
Upvotes: 1