Way to find if function will return promise

后端 未结 3 1359
花落未央
花落未央 2020-12-20 01:34

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         


        
3条回答
  •  隐瞒了意图╮
    2020-12-20 02:07

    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.

提交回复
热议问题