Search code examples
react-nativenested-function

In nested functions only the first function works


I've never used the nested functions before, and this seemingly simple task is giving me trouble. When I run this code only the first function work, and the second is completely unresponsive.

const getCMSID = () => {
    let url = `${URL}/store-api/category`;
    let getId = {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Accept: 'application/json',
        'sw-access-key': `${API_KEY}`,
      },
    };
    fetch(url, getId)
      .then(res => res.json())
      .then(json => console.log(json.elements[0].cmsPageId))
      .catch(err => console.error('error:' + err));

    const getCMSPage = () => {
      const cmsUrl = `${URL}/store-api/${id}`;
      let getcms = {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          Accept: 'application/json',
          'sw-access-key': `${API_KEY}`,
        },
      };
      fetch(cmsUrl, getcms)
        .then(res => res.json())
        .then(json => {
          console.log(json.sections);
          setLoading(false);
        })
        .catch(err => console.error('Error:' + err));
    };
  };

Solution

  • It doesnt look like you ever call getCMSPage

    const getCMSID = () => {
        let url = `${URL}/store-api/category`;
        let getId = {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
            Accept: 'application/json',
            'sw-access-key': `${API_KEY}`,
          },
        };
        fetch(url, getId)
          .then(res => res.json())
          .then(json => console.log(json.elements[0].cmsPageId))
          .catch(err => console.error('error:' + err));
    
        const getCMSPage = () => {
          const cmsUrl = `${URL}/store-api/${id}`;
          let getcms = {
            method: 'POST',
            headers: {
              'Content-Type': 'application/json',
              Accept: 'application/json',
              'sw-access-key': `${API_KEY}`,
            },
          };
          fetch(cmsUrl, getcms)
            .then(res => res.json())
            .then(json => {
              console.log(json.sections);
              setLoading(false);
            })
            .catch(err => console.error('Error:' + err));
        };
        getCMSPage()
      };