Created
November 13, 2019 12:38
-
-
Save sagarpanchal/1504709f60198a256a4fe9e580c606f0 to your computer and use it in GitHub Desktop.
JS Commons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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