promise笔记之thenable 对象

4,047 阅读1分钟

#thenable是一个对象或者函数。

thenable 对象的作用: 使promise的实现更具有通用性

识别thenable或行为类似Promise对象可以根据其是否具有then(...)方法来判断,这叫类型检查(鸭式辩型)。

thenable的鸭式类型检测:

        if(p !==null && 
            (
            typeof p === 'object' ||
            typeof p === 'function'
            )&& 
            typeof p.then === 'function'
        ){
            //thenable
        }else{
            //非thenable
        }

轻汗微微透碧纨。