Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

How to stop a request in Axios?

$
0
0

I would like to stop a request when the database is offline or when there is a problem with the backend conexion, but I don't know how to do this.

My Axios settings are as follows:

import axios from "axios";import AsyncStorage from '@react-native-async-storage/async-storage';const baseURL = 'http://192.168.1.43:4000/server';const gestorMedicoApi = axios.create({ baseURL});gestorMedicoApi.interceptors.request.use(    async (config) => {        const token = await AsyncStorage.getItem('token');        if (token) {            config.headers!['x-token'] = token;        }        return config;    })export default gestorMedicoApi;

And an request example is the following:

    const signUp = async ({ nombreEmpresa, email, telefono, contraseña }: RegisterData)  => {        try {            const resp = await gestorMedicoApi.post<Response>('/user/createUser', { nombreEmpresa, email, telefono, contraseña });             dispatch({                type: 'signUp',                payload: {response: resp.data}            })        } catch (error: any) {            dispatch({ type: 'addError', payload: {response: {status: "NOK", message: "Se ha producido un error al conectar con la base de datos."}} })        }    };

If the next line fails:

 const resp = await gestorMedicoApi.post<Response>('/user/createUser', { nombreEmpresa, email, telefono, contraseña });

The flow go into the catch, but the request tries to conect again and again, so I would like to stop the request if it fails.

How can I get it?


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>