Custom soables:我可以使用“then”创建自己的对象吗?方法? [英] Custom thenables: Can I create my own objects with a "then" method?

查看:124
本文介绍了Custom soables:我可以使用“then”创建自己的对象吗?方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道自定义的thenables(也称为具有 .then()方法的对象)是否被批准/记录?他们如何与真正的承诺相提并论?我认为你的实施方式很重要,所以我想知道是否有一些DO和DONT的文档。

I'm wondering if custom "thenables" (aka an object with a .then() method) are approved/documented? 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.

此页建议:


。然后可能会返回任意 对象,它将被视为与承诺相同的方式。

.then may return an arbitrary "thenable" object, and it will be treated the same way as a promise.

找不到任何关于此的文档。

Can't find any docs on this.

推荐答案


theables与真实Promise相比如何?

How do thenables compare to real Promises?

因为你不知道他们是否是真正的承诺。看一下关于Promises / A +规范,这两个术语之间的区别是什么?和承诺?

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"?.


我想知道是否有关于如何实施它们的DO和DONT的文档(因为这似乎是重要的)

I'm wondering if there's documentation with some DOs and DONTs about how to implement them (as that seems to be what matters)

有(非常简单) Promises / A +规范,记录了如何处理。 ES6承诺(以及扩展名等待)遵循此。基本上:

There's the (pretty simple) Promises/A+ specification that documents how thenables are treated. ES6 promises (and by extension, await) follow this. Basically:


  • 你的对象有一个名为的属性,然后是,其值为函数

  • 函数将被调用两个回调

  • 你可以调用其中任何一个 - 异步或不是

  • 第一个电话决定了同化你的可能性的承诺会发生什么

  • your object has a property with the name then whose value is a function
  • the function will get called with two callbacks
  • you can call either of them - asynchronously or not
  • the first call determines what happens to the promise that assimilated your thenable

这真的没有魔力。当您想要使用值解析时调用第一个参数,并在要拒绝时调用第二个参数。没有DONT假设正确的承诺实现 - 您可以根据需要随时调用回调,并且只要您愿意保留它们:应该忽略调用并且引用不应该泄漏内存。

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.

这篇关于Custom soables:我可以使用“then”创建自己的对象吗?方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆