Problem with conditionally returning promise (Nodejs)

This question may require a title for editing.

Hi, I have a method that returns an object or promise. The function is as follows:

 function getAllTypes()
 {
   if(cache_hit)
   return from_cache;

   else
   {
    // db Query
    findAllTypes.then(function(result){
    return result;
    }
   }
}

This function will either return a promise or a simple JS object, as appropriate. This is a utility function and will be used in many different places. I would like to keep a constant return type, be it a promise or a simple JS object (which does not need permission) in both cases.

Is there any way to do this?

+4
source share
1 answer

- , , , , , Promise Promise.resolve, ..

function getAllTypes() {
  return cache_hit ? return Promise.resolve(from_cache) : findAllTypes()
}
+1

All Articles