-2

I'm working on two functions about my project. First function is async function:

async function abc(params) {
(...)
var sdata = JSON.stringify(params);
fetch(...)
    .then(response => response.json())
    .then(data => {
        /*do something*/
    })
    .catch(err => {
        /*err do something*/
    })
}

Second function is:

function xyz (param){
    irrevelantFunction(param);
}

I tried to execute this two functions like below:

abc(params).then(xyz(param));

but it doesn't work. How can I solve this problem? Many thanks for your precious support.

1 Answers1

-1

If you are using async then you can avoid .then, kindly see the sample snippet code

async function abc(params) {
var sdata = JSON.stringify(params);
try{
  const response = await fetch(...)
  if(response.ok){
    const body = await response.json();
    xyz(body)
    return
  }
  const cusotomError = {
   message: 'something went wrong'
  }
  throw customError
}catch(error){
  console.log(error) // you can show error to ui logic
}



function xyz (param){
    irrevelantFunction(param);
}
Learner
  • 8,379
  • 7
  • 44
  • 82