I came across this code recently and I'm curious about why it specifies the return type as Promise
. Since the function is returning data, which is an object, what's the purpose of adding | null
at the end?
const getSomething = async (
id: string
): Promise<UserData | null> => {
try {
const { data } = await axios.get(
`${API}/user?id=${id}`
);
return data;
} catch (err) {
if (err.response) {
return err.response.data;
}
return null;
}
};