1

I currently have a method called isSpecialStatus() which has a simple return function

return (this.getStatus() === 'Special')

getStatus is also a simple return function with some conditionals

if (someArray.includes('SECRET_VALUE') {
    return 'Special';
} else if (someArray.includes('OTHER_VALUE') // logic that's not relevant to this question

Instead of having all these conditional statements (the actual logic in the conditionals checks more than just one value)- we are using asynchronous calls to the DB that holds a list of values for each condition. So we can do

this.getSpecialValueList().then(results => console.log(results) // output: 'SECRET_VALUE'

however my isSpecialStatus() is used throughout the codebase and I need it to return a boolean true/false instead of a promise that resolves into a true/false. I've tried using async/await and using promise chaining but no matter what- including the promise in isSpecialStatus results in the output being a promise, not a boolean.

I have looked at other SO posts and have seen people recommend using outside variables and setting them and calling isSpecialStatus in my case from within getStatus() and it should return normally- which is fine- but I don't really want to add any global variables to my code if I can avoid it and the current scope of these methods would require the variable to be global on the window object.

Are there any alternatives other than promisifying all my methods that use isSpecialStatus() or using global variables?

Adam S
  • 53
  • 1
  • 5
  • 1
    Server side (node.js) you could theoretically use something like deasync, but this is really a dirty hack and should be avoided. Otherwise no, there's no other way. Once you go async, you have to be async everywhere. – georg May 15 '19 at 19:59
  • I realy can not follow. if `isSpecialStatus()` does not need async execution, why should it return a Promise? Solely from the return statement it is not certain what type you will return. If the function is prefixed with `async` it will always return a Promise. – Bellian May 15 '19 at 20:10
  • @Bellian When I was using `then()` statements and returning in the `then()` I was getting undefined. It could be a made a minor bug with my implementation of then chaining. Are you saying its possible to have a then chain inside of a method and return a string value when using the method elsewhere? I can put my attempt inside the body if you think it is possible – Adam S May 15 '19 at 20:32
  • Does this answer your question ? https://stackoverflow.com/a/47227878/438970 – Damien May 15 '19 at 20:41
  • "*I need it to return a boolean true/false instead of a promise that resolves into a true/false*" - that's simply impossible if your `isSpecialStatus()` function has to do something asynchronous like a database request. – Bergi May 15 '19 at 21:03

0 Answers0