Skip to content

Instantly share code, notes, and snippets.

@sagarpanchal
Created November 13, 2019 12:38
Show Gist options
  • Save sagarpanchal/1504709f60198a256a4fe9e580c606f0 to your computer and use it in GitHub Desktop.
Save sagarpanchal/1504709f60198a256a4fe9e580c606f0 to your computer and use it in GitHub Desktop.
JS Commons
interface IAjaxCall {
error: boolean;
status: number;
data: any;
}
/**
* async api call helper
* @param func function that'll return promise
*/
export const ajaxCall = async (func: Function): Promise<IAjaxCall> => {
try {
const { status, data } = await func();
return { error: false, status, data };
} catch (e) {
if (e.hasOwnProperty('response')) {
const { status, data } = e.response;
return { error: true, status, data };
} else {
return { error: true, status: 500, data: 'Something went wrong' };
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment