[英]Custom thenables: Can I create my own objects with a “then” method?
I'm wondering if custom "thenables" (aka an object with a .then()
method) are approved/documented? 我想知道是否已批准/记录了自定义“ thenables”(又名带有.then
.then()
方法的对象)? How do they compare to real Promises? 它们与真实的承诺相比如何? I suppose it matters how you implement then, so I'm wondering if there's documentation with some DOs and DONTs.
我想这如何实现很重要,所以我想知道是否有一些DO和DONT的文档。
.then may return an arbitrary “thenable” object, and it will be treated the same way as a promise.
.then可以返回任意“ thenable”对象,并且将其与promise一样对待。
Can't find any docs on this. 找不到与此相关的任何文档。
How do thenables compare to real Promises?
事物与真实的应许相比如何?
In that you don't know whether they are real promises or not. 因为您不知道它们是否是真实的承诺。 Have a look at Regarding Promises/A+ Specification, what is the difference between the terms "thenable" and "promise"?
看一下Promises / A +规范,术语“ thenable”和“ promise”有什么区别? .
。
I'm wondering if there's documentation with some DOs and DONTs about how to implement them (as that seems to be what matters)
我想知道是否有一些DO和DONT的文档说明如何实现它们(这似乎很重要)
There's the (pretty simple) Promises/A+ specification that documents how thenables are treated. 有一个(相当简单的) Promises / A +规范 ,记录了如何处理ableable。 ES6 promises (and by extension,
await
) follow this. ES6承诺(并且扩展为
await
)将遵循此要求。 Basically: 基本上:
then
whose value is a function then
它的值是一个函数 It's really no magic. 真的没有魔术。 You call the first argument when you want to resolve with a value, and you call the second argument when you want to reject.
要使用值进行解析时,请调用第一个参数,而要拒绝时,请调用第二个参数。 There are no DONTs assuming a proper promise implementation - you can call the callbacks as often as you want, and keep them around as long as you want: the calls should be ignored and the reference should not leak memory.
没有DONT会假定适当的Promise实现-您可以根据需要多次调用回调,并可以根据需要保留它们很长时间:应忽略调用,并且引用不应泄漏内存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.