Reputation: 17332
This is how my upload function looks like at the moment. I'm using apollo mutation in that to upload a file.
I do not understand how to use try/catch and catch of the promise (which client.mutate()
is) correctly.
Also I declared the upload function as async
.
So I guess I'm mixing some things up :-(
How do I catch errors correctly?
Do I need both catches? Shouldn't I replace try/catch if I'm using a async function?
export default async function upload (client) {
try {
return client.mutate({
mutation: uploadsMutation
}).catch(err => {
console.error(err)
})
} catch (error) {
Alert.alert('Error', 'Could not upload files')
}
}
Upvotes: 0
Views: 1623
Reputation: 151
async
and await
have to be used hand in hand - meaning nothing is automatically 'awaited' without using the await
keyword. In your example you're just returning the Promise returned from client.mutate
.
export default async function upload (client) {
try {
return await client.mutate({
mutation: uploadsMutation
});
} catch (error) {
Alert.alert('Error', 'Could not upload files')
}
}
Bear in mind your upload
function is also returning a Promise by being async
. So calling code should handle it appropriately.
Upvotes: 2