Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
Javascript : function that involves two functions

I'm trying to make a function call "sendAndMove" that involves two function inside. And I want two functions executed in order of setting() ---> goHome(). But when I wrote code like down below, it is exectued like goHome() ---> setting(). So I used async/await to achieve to goal.

Is this problem happend because of asynchronous feature and Call stack? And I want to know why setting() function doesn't work if page changes. Thanks a lot

function setting() {
        setFinalInfo((prev)=>{return{...prev,
          finalAddress: info.address,
          finalCode: info.code,
          finalDetailAddress: info.detailAddress,
          finalPostCode: info.postCode,
          finalExtraAddress: info.extraAddress}
        });
          
      }
      function goHome(){
        navigate("/")
      }
        
      function sendAndMove() {
        setting();
        goHome();
          
      }

So I Changed to code below.

  function setting() {
        setFinalInfo((prev)=>{return{...prev,
          finalAddress: info.address,
          finalCode: info.code,
          finalDetailAddress: info.detailAddress,
          finalPostCode: info.postCode,
          finalExtraAddress: info.extraAddress}
        });
          
      }
      function goHome(){
        navigate("/")
      }
        
      async function sendAndMove() {   ***** Changed this part
        await setting();
        await goHome();
          
      }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

What you actually want to do is 'channel' from a callback to an awaitable promise, something like this:

function setting() {
  return new Promise((resolve) => {
    setFinalInfo((prev) => {
      resolve({
        ...prev,
        finalAddress: info.address,
        finalCode: info.code,
        finalDetailAddress: info.detailAddress,
        finalPostCode: info.postCode,
        finalExtraAddress: info.extraAddress,
      });
    });
  });
}

function goHome() {
  navigate('/');
}

async function sendAndMove() {
  await setting();
  goHome();
}

Btw, you don't need to await on goHome() since that is not an async function.

about 4 years ago · Juan Pablo Isaza Report

0

The optimal approach would be to set the state, and then use useEffect to watch for a change in state and then call navigate.

function update() {
  setFinalInfo((prev) => {
    return { ...prev,
      finalAddress: info.address,
      finalCode: info.code,
      finalDetailAddress: info.detailAddress,
      finalPostCode: info.postCode,
      finalExtraAddress: info.extraAddress
    }
  });
}

useEffect(() => {
  navigate('/');
}, [finalInfo]), 

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!