skip to Main Content

let response = data?.is_signup ? await apiRequest.Auth.sendLoginOtp(data) : await apiRequest.Auth.login(data);

condition based i want trigger different api

2

Answers


  1. You can handle API requests conditionally in React based on certain conditions. The code snippet you provided looks mostly correct, assuming data is the object containing the necessary parameters for your API requests and is_signup is a boolean value indicating whether it’s a signup operation or not.

        async function handleAPIRequest(data) {
      let response;
      if (data?.is_signup) {
        response = await apiRequest.Auth.sendLoginOtp(data);
      } else {
        response = await apiRequest.Auth.login(data);
      }
      // Now you can handle the response as needed
    }
    

    you can call handleAPIRequest(data) whenever you need to make the API request, passing in the appropriate data object.

    Make sure apiRequest.Auth.sendLoginOtp and apiRequest.Auth.login are defined correctly and return Promises, as await expects a Promise to be returned from an async function.

    Login or Signup to reply.
  2. async function makeRequest(data) {
    

    let response;
    try {
    return data.islogin ? await apiRequest.Auth.sendLoginOtp(data) :await apiRequest.Auth.login(data);;
    } catch (e) {
    return {
    error: e
    }
    }

    }

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search