Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const networkCall = async ({ path = '', method = '', body = {} }) => {
- const controller = new AbortController();
- const { signal } = controller;
- const req = { method, headers: { 'Content-Type': 'application/json' }, signal };
- if (method !== 'GET') {
- req.body = JSON.stringify(body);
- }
- const responseInfo = { done: false };
- const response = await Promise.race([
- fetch(path, req).then((res) => {
- responseInfo.done = true;
- return res.json();
- }),
- new Promise((resolve) => {
- setTimeout(() => {
- resolve({ error: `Bad connection ('${path}')` });
- if (!responseInfo.done) {
- controller.abort();
- }
- }, 15000);
- }),
- ]);
- return response;
- };
Add Comment
Please, Sign In to add comment