-1

I want to use my fetched API result outside of the function.

apiFetch({
      url: lmn_admin.ajax_url,
      method: "POST",
      body: formData,
    })
      .then((result) => {
        setIsLoading(false);
        setIsSuccess(true);
        console.log(result);
      })
      .catch((error) => {
        setIsLoading(false);
        setIsError(true);
        console.error(error);
      });
  • 1
    Please explain what you're trying to do and what's failing. If you're trying to access the result of a promise from elsewhere, you can return it in the `.then` and then [use it in some other async function or .then](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323#14220323) – Zac Anger Jul 27 '23 at 02:17
  • Welcome to stackoverflow. Please read [How to ask](https://stackoverflow.com/help/how-to-ask) – Ricky Mo Jul 27 '23 at 03:04

1 Answers1

-1
const [dataResult, setDataResult] = useState(null)

apiFetch({
      url: lmn_admin.ajax_url,
      method: "POST",
      body: formData,
    })
      .then((result) => {
        setDataResult(result);
        setIsLoading(false);
        setIsSuccess(true);
      })
      .catch((error) => {
        setIsLoading(false);
        setIsError(true);
        console.error(error);
      });

console.log(dataResult && dataResult)
  • Code-only answers tend to be much less helpful than answers including some written explanation. Consider editing your answer to explain how your code could be useful to the asker. – Cat Jul 27 '23 at 03:25
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 30 '23 at 11:10