-3

As parent function does not support async. I need to make call without await and get return value. As suggested on most of the posts, applied promise with then and returning value. But it prints as "promise" instead of return "value".

Can you please share, how to achieve return value instead of promise.

code sandbox: https://codesandbox.io/s/await-without-async-and-return-value-qbs7t?file=/src/index.js


await isEnable(data) {
  try {
    ...
    return true;
  }
  catch (e) {
    console.error(e);
  }
}
}

const getEnableStatus=(data) =>{
  return isEnable(data).then((result) =>
    {console.log(result); return result;});  //this prints correctly but, need this in return result.
}

console.log(getEnableStatus(data)); //it always print 'promise'. how to get value here instead of promise.
  • 1
    That's exactly what's supposed to happen. If you want to get the value a promise resolves to, you need to either `await` it or use `.then`. – jonrsharpe Sep 29 '21 at 15:54
  • Yes, that make sense. But actually already using 'then' and it prints promise (at console.log).. – user16451645 Sep 29 '21 at 15:55
  • And more to the point, the "return result" is happenning in the function you pass the then(), it has nothing to do with the function getEnableStatus, which is (correctly) returning the promise. You're trying to make a synchromous function return the result of an async function, and that's just not possible: what if the promise doesn't resolve for minutes? Do you want the whole system to just freeze and wait? – Lee Daniel Crocker Sep 29 '21 at 15:56
  • There are syntax errors in your code--- you have an incorrect number of braces, and you appear to be `await`-ing a function declaration that lacks a `function` keyword? – Alexander Nied Sep 29 '21 at 15:56
  • Using `.then` gives you access to the resolved value _inside the callback_, the result is still a promise. – jonrsharpe Sep 29 '21 at 15:56
  • @jonrsharpe yes, that's what happening with `.then`. how to `return value` instead of promise ? – user16451645 Sep 29 '21 at 15:58
  • 2
    **You can't**, refer back to https://stackoverflow.com/questions/69379683/return-value-instead-of-promise-without-await?noredirect=1#comment122628208_69379683 – jonrsharpe Sep 29 '21 at 15:59
  • Ok. Is it possible to resolve within `getEnableStatus(data) ` function and return directly value. (To hold till result received). – user16451645 Sep 29 '21 at 16:17
  • 1
    @user16451645 No, not possible. – Ivar Sep 29 '21 at 16:23
  • ok. Just added code sandbox if that possible with self executing async function or "then" in between proxy function. – user16451645 Sep 29 '21 at 18:49

1 Answers1

0

Pass in a callback to the getEnableStatus function, and then call it with the returned data.

// Mock function that resolves after
// two seconds
function isEnable() {
  return new Promise((res, rej) => {
    setTimeout(() => res('Hallo!'), 2000);
  });
}

// Accept a callback, and call it with the data
function getEnableStatus(data, callback) {
  isEnable(data).then(callback);
}

// The callback logs the data
getEnableStatus('data', function (data) {
  console.log(data);
});
Andy
  • 61,948
  • 13
  • 68
  • 95
  • 1
    Why not just use the same callback _in `.then`_? And note `getEnableStatus` now returns a promise of `undefined` - the convention is to return a promise _or_ take a callback, and if you're going to return a promise it should resolve to the value. – jonrsharpe Sep 29 '21 at 16:04
  • Fair points well made @jonrsharpe. – Andy Sep 29 '21 at 16:08
  • Thanks. I can't change the signature of `getEnableStatus(data)` , just because, its tight to another component. and That component just read the value as it is return from `getEnableStatus(data)` this function so, I can't do `then` after returning result. Need to do something within `getEnableStatus(data)` function to return `value`. – user16451645 Sep 29 '21 at 16:14
  • You say @user16451645, that "The parent function doesn't support await". Does that mean you can't the code for that either? – Andy Sep 29 '21 at 16:37
  • Yes, `getEnableStatus` function is reference as parameter on a component from npm package which expected as normal function parameter and can't control the signature or logic of that function. so, need to resolve/return value from `getEnableStatus(data)` function. And it will be use further from component. – user16451645 Sep 29 '21 at 17:46
  • https://codesandbox.io/s/await-without-async-and-return-value-qbs7t?file=/src/index.js – user16451645 Sep 29 '21 at 18:14