Skip to main content
removed unnecessary usage of async
Source Link
NeERAJ TK
  • 2.7k
  • 1
  • 18
  • 34

You can move the second request to the then block of the dependent first request,i.e., getTracks. Also, as:you shouldn't mix then and await.

useEffect(() => {
    const getTracks = async () => {
        httpClient.get(`/track/${id}`)
            .then((response) => {
                setTrack(response.data);
                httpClient.get(`/profile/${response.data.user}/`)
               .then((response) => {
                 setUser(response.data);
               })
            })
    }
    getTracks();
}, [])

You can move the second request to the then block of the dependent first request,i.e., getTracks, as:

useEffect(() => {
    const getTracks = async () => {
        httpClient.get(`/track/${id}`)
            .then((response) => {
                setTrack(response.data);
                httpClient.get(`/profile/${response.data.user}/`)
               .then((response) => {
                 setUser(response.data);
               })
            })
    }
    getTracks();
}, [])

You can move the second request to the then block of the dependent first request,i.e., getTracks. Also, you shouldn't mix then and await.

useEffect(() => {
    const getTracks = () => {
        httpClient.get(`/track/${id}`)
            .then((response) => {
                setTrack(response.data);
                httpClient.get(`/profile/${response.data.user}/`)
               .then((response) => {
                 setUser(response.data);
               })
            })
    }
    getTracks();
}, [])
deleted 12 characters in body
Source Link
NeERAJ TK
  • 2.7k
  • 1
  • 18
  • 34

You can move the second request to the then block of the dependent first request,i.e., getTracks, as:

useEffect(() => {
    const getTracks = async () => {
        await httpClient.get(`/track/${id}`)
            .then((response) => {
                setTrack(response.data);
                await httpClient.get(`/profile/${response.data.user}/`)
               .then((response) => {
                 setUser(response.data);
               })
            })
    }
    getTracks();
}, [])

You can move the second request to the then block of the dependent first request,i.e., getTracks, as:

useEffect(() => {
    const getTracks = async () => {
        await httpClient.get(`/track/${id}`)
            .then((response) => {
                setTrack(response.data);
                await httpClient.get(`/profile/${response.data.user}/`)
               .then((response) => {
                 setUser(response.data);
               })
            })
    }
    getTracks();
}, [])

You can move the second request to the then block of the dependent first request,i.e., getTracks, as:

useEffect(() => {
    const getTracks = async () => {
        httpClient.get(`/track/${id}`)
            .then((response) => {
                setTrack(response.data);
                httpClient.get(`/profile/${response.data.user}/`)
               .then((response) => {
                 setUser(response.data);
               })
            })
    }
    getTracks();
}, [])
Source Link
NeERAJ TK
  • 2.7k
  • 1
  • 18
  • 34

You can move the second request to the then block of the dependent first request,i.e., getTracks, as:

useEffect(() => {
    const getTracks = async () => {
        await httpClient.get(`/track/${id}`)
            .then((response) => {
                setTrack(response.data);
                await httpClient.get(`/profile/${response.data.user}/`)
               .then((response) => {
                 setUser(response.data);
               })
            })
    }
    getTracks();
}, [])