1

What is the way to take data from getUserConnectRequestData function and pass it to getUserConnectResponseData function ? as you can see so i try to use then and responseData to for save the data of the getUserConnectRequestData function and than i try pass it into the getUserConnectResponseData function but itd not works .

getUserConnectRequestData().then(() => {
          responseData();
        });

and this is getUserConnectResponseData function that i want to pass the data from getUserConnectRequestData

export const getUserConnectResponseData = (responseData) => {
  return new Promise((resolve, reject) => {
    // console.log('THIS IS MY RESPONSE ==============>>>>>>>>>>>', responseData);
    try {
      fetch(
        'https://hghghgghghg3223223',
        {
          method: 'POST',
          headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({
            Req_Type: responseData.Req_Type,
            Language_Code: responseData.Language_Code,
            User_ID: responseData.User_ID,
            Session_ID: responseData.Session_ID,
            Session_Key: responseData.Session_Key,
            Client_Type: responseData.Client_Type,
            Req_Data: {
              Bridge_ID: responseData.Bridge_ID,
            },
          }),
        }
      )
        .then((response) => response.json())
        .then((jsonResponse) => {
          resolve(jsonResponse);
        });
    } catch (error) {
      reject(error);
    }
  });
};
shira
  • 374
  • 4
  • 21
  • 1
    Also note this: [What is the explicit promise construction antipattern and how do I avoid it?](https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it) –  Aug 04 '20 at 07:16

1 Answers1

0

You need to accept the parameter and use it, and call the right function:

getUserConnectRequestData().then((responseData) => {
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−^^^^^^^^^^^^
    getUserConnectResponseData(responseData);
// −^^^^^^^^^^^^^^^^^^^^^^^^^^−^^^^^^^^^^^^
});

But since getUserConnectResponseData takes just that one parameter you know that the then callback will only be called with that one single argument:

getUserConnectRequestData().then(getUserConnectResponseData);

You also need to handle errors, so:

getUserConnectRequestData()
.then(getUserConnectResponseData)
.catch(error => {
    // Handle/report error
});

There are a couple of other things to point out, though:

  1. getUserConnectRequestData is falling prey to a promise anti-pattern: You don't need new Promise when you already have a promise (from fetch) to use.

  2. You need to check for HTTP success before calling .json() on the response. Sadly, fetch only rejects on network errors, not HTTP errors.

Here's an updated version of getUserConnectRequestData:

export const getUserConnectResponseData = (responseData) => {
    return fetch('https://hghghgghghg3223223', {
        method: 'POST',
        headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            Req_Type: responseData.Req_Type,
            Language_Code: responseData.Language_Code,
            User_ID: responseData.User_ID,
            Session_ID: responseData.Session_ID,
            Session_Key: responseData.Session_Key,
            Client_Type: responseData.Client_Type,
            Req_Data: {
                Bridge_ID: responseData.Bridge_ID,
            },
        }),
    })
    .then((response) => {
        if (!response.ok) {
            throw new Error("HTTP error " + response.status);
        }
        return response.json();
    });
};

Because of that need for the check, I never use fetch directly, I have wrappers to do the check so I don't have to code it Every Single Time.

// General purpose
function fetchGeneral(...args) {
    return fetch(...args)
    .then((response) => {
        if (!response.ok) {
            throw new Error("HTTP error " + response.status);
        }
        return response;
    });
}

// JSON
function fetchJSON(...args) {
    return fetch(...args)
    .then((response) => {
        if (!response.ok) {
            throw new Error("HTTP error " + response.status);
        }
        return response.json();
    });
}

Those reject on both network and HTTP errors.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • FWIW, I go into promises (including useful patterns and warnings of anti-patterns) in Chapter 8 of my new book. Check my profile for details if you're interested. – T.J. Crowder Aug 04 '20 at 07:21