-2

So I am 99% sure this cannot be done, but humor me for a moment. Consider the two functions:

function doAjaxCall(fieldTocheckAgainst, currentField, value) {
  axios.get(
    window.location.origin +
    '/api/clinic/'+window.id+'/patient/'+window.secondId+'/field-validation',
    {
      params: {
        field_to_check_against: fieldTocheckAgainst,
        current_field: currentField,
        value: moment(value).format('YYYY-MM-DD')
      }
    }
  ).then((result) => {
    return result.data
  });
}

async function resolveAjaxCall(fieldTocheckAgainst, currentField, value) {
  const result = await doAjaxCall(fieldTocheckAgainst, currentField, value)

  console.log(result);
}

I am attempting to resolve the axios ajax call into a variable based on what I saw here and it doesn't work. I get undefined.

I understand when it comes to callbacks, everything has to be done in the callback, but is there no way, with async and await to resolve the promise to a variable as I am attempting to do, to then use said variable else where?

Or am I just going to be stuck with callbacks?

TheWebs
  • 12,470
  • 30
  • 107
  • 211

2 Answers2

1

The only issue I see with your code is that axios.get() returns a Promise, but you aren't returning that promise from doAjaxCall. Promises are values, and they need to be passed around to be used.

Just add return before axios.get and that should give you the result you want.

--

On the question of "returning" from a promise - Async functions always return promises, even if you don't include a return statement. If your async function ends in the statement return true, it's actually returning a Promise that resolves to true. As you guessed in your original post, there is no way to pull a value out of a promise and into synchronous code.

backtick
  • 2,685
  • 10
  • 18
  • that worked, accept now I get: `Promise {}` and then `{message: "Date cannot be greator then today.", isError: true}` Is there something I am missing such that I only get the object and not the promise then the object? – TheWebs Jul 09 '19 at 01:01
  • Async functions always return promises - even if you don't include a return statement. If your async function ends in the statement `return true`, it's actually returning a Promise that resolves to `true`. As you guessed in your original post, there is no way to pull value out of a promise and into synchronous code. – backtick Jul 09 '19 at 01:12
1

You are missing the return statement in your doAjaxCall function, and you also need to treat the function as promise. It should be the following:

function doAjaxCall(fieldTocheckAgainst, currentField, value) {
    return axios.get(
        window.location.origin + '/api/clinic/'+window.id+'/patient/'+window.secondId+'/field-validation',
        {
            params: {
            field_to_check_against: fieldTocheckAgainst,
            current_field: currentField,
            value: moment(value).format('YYYY-MM-DD')
        }
    }).then((result) => {
        return result.data
    });
}

async function resolveAjaxCall(fieldTocheckAgainst, currentField, value) {

    const result = doAjaxCall(fieldTocheckAgainst, currentField, value).then(data = {
        console.log(data);
    })

}
Ben Carey
  • 16,540
  • 19
  • 87
  • 169
  • The problem is I am trying to avoid using the `.then` because the `resolveAjaxCall` should return just the result of the ajax call. I assume there is no way around that? – TheWebs Jul 09 '19 at 01:06