本文翻译自:How do I tell if an object is a Promise?
Whether it's an ES6 Promise or a bluebird Promise, Q Promise, etc. 无论是ES6 Promise还是bluebird Promise,Q Promise等。
How do I test to see if a given object is a Promise? 如何测试以查看给定对象是否为Promise?
#1楼
参考:https://stackoom.com/question/1sQ52/我如何知道一个对象是否是一个承诺
#2楼
How a promise library decides 承诺库如何决定
If it has a .then
function - that's the only standard promise libraries use. 如果它具有.then
函数-这是.then
库唯一使用的标准。
The Promises/A+ specification has a notion called then
able which is basically "an object with a then
method". Promises / A +规范具有一个称为then
的概念,该概念基本上是“带有then
方法的对象”。 Promises will and should assimilate anything with a then method. 承诺会并且应该使用then方法吸收任何东西 。 All of the promise implementation you've mentioned do this. 您提到的所有promise实现都可以做到这一点。
If we look at the specification : 如果我们看一下规格 :
2.3.3.3 if
then
is a function, call it with x as this, first argument resolvePromise, and second argument rejectPromise 2.3.3.3如果then
是一个函数,则用x作为其调用,第一个参数resolvePromise,第二个参数rejectPromise
It also explains the rationale for this design decision: 它还说明了此设计决策的基本原理:
This treatment of
then
ables allows promise implementations to interoperate, as long as they expose a Promises/A+-compliantthen
method. 只要能够公开Promises / A +兼容then
方法,这种对能力的处理then
可以使promise实现互操作。 It also allows Promises/A+ implementations to “assimilate” nonconformant implementations with reasonable then methods. 它还允许Promises / A +实现使用合理的then方法“整合”不合格的实现。
How you should decide 您应该如何决定
You shouldn't - instead call Promise.resolve(x)
( Q(x)
in Q) that will always convert any value or external then
able into a trusted promise. 你不应该-改为调用Promise.resolve(x)
Q(x)
在Q),将永远转换任何值或外部then
能够成为一个值得信赖的承诺。 It is safer and easier than performing these checks yourself. 比您自己执行这些检查更安全,更轻松。
really need to be sure? 真的需要确定吗?
You can always run it through the test suite :D 您始终可以通过测试套件来运行它:D
#3楼
if (typeof thing.then === 'function') {
// probably a promise
} else {
// definitely not a promise
}
#4楼
Checking if something is promise unnecessarily complicates the code, just use Promise.resolve
检查是否允许不必要的复杂代码,只需使用Promise.resolve
Promise.resolve(valueOrPromiseItDoesntMatter).then(function(value) {
})
#5楼
Update: This is no longer the best answer. 更新:这不再是最佳答案。 Please vote up my other answer instead. 请改选其他答案 。
obj instanceof Promise
should do it. 应该这样做。 Note that this may only work reliably with native es6 promises. 请注意,这只能与本机es6 Promise可靠地一起使用。
If you're using a shim, a promise library or anything else pretending to be promise-like, then it may be more appropriate to test for a "thenable" (anything with a .then
method), as shown in other answers here. 如果您使用的垫片,一个承诺库或其他任何故作承诺般,那么它可以为“thenable”(有什么更合适的测试.then
方法),如在其他的答案在这里。
#6楼
Here is the code form https://github.com/ssnau/xkit/blob/master/util/is-promise.js 这是代码形式https://github.com/ssnau/xkit/blob/master/util/is-promise.js
!!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
if an object with a then
method, it should be treat as a Promise
. 如果对象带有then
方法,则应将其视为Promise
。