Skip to content

Instantly share code, notes, and snippets.

@FreddyPoly
Created June 28, 2019 16:19
Show Gist options
  • Save FreddyPoly/7f2788fe9c7de9657e923f218dba0b32 to your computer and use it in GitHub Desktop.
Save FreddyPoly/7f2788fe9c7de9657e923f218dba0b32 to your computer and use it in GitHub Desktop.
[REACT NATIVE] API Service Example
import * as env from "../../environment-variables"
export default {
get() {
let didTimeout = false;
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
didTimeout = true;
reject(new Error('Request timed out'));
}, 30000);
fetch(`[ROUTE]${env.API_BASE_AIRTABLE}?api_key=${env.API_KEY_AIRTABLE}`, {
method: 'GET',
headers: {
'content-type': 'application/json'
}
})
.then(async (resp) => {
// Ne pas continuer si la requête a Timeout
if (didTimeout) return;
// La requête n'a pas time out donc clear
clearTimeout(timeout);
if (!didTimeout) {
// La requête n'a pas timeout et s'est bien déroulée
resolve(resp);
}
})
.catch((err) => {
// La requête a échouée ou bien a timeout
if (didTimeout) return;
reject(err);
});
});
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment