Below I have a function that returns a promise that resolves true
. Is there any way I can find out if a function will return a promise?
var myPr
There is no good way to do this, but you can call the methods and inspect their return values.
function willReturnPromise(fn) {
const result = fn();
return result && typeof result.then === 'function';
}
The A+ Promise spec does not require promises to have anything other than a function named then
, so this is the best you can do if you want the function to work with all Promise implementations.
Calling the function and throwing away the result just to determine this is not a good idea though.