简体   繁体   English

在“ Promise.then()”中返回带有“ then”功能的对象

[英]Return an object with a “then” function within “Promise.then()”

Within my Node.JS application I have written a function ( findByReference ) that goes to a database and asynchronously yields a fetched database row. 在我的Node.JS应用程序中,我编写了一个函数( findByReference ),该函数转到数据库并异步产生已获取的数据库行。 I have written this function using Promises. 我已经使用Promises编写了此功能。 Additionally, I have written an implementation of the Maybe monad and want my findByReference function to yield an instance of Maybe . 另外,我已经编写了Maybe monad的实现,并希望我的findByReference函数产生Maybe的实例。

My code looks like the below: 我的代码如下所示:

findByReference(r)
  .then(raw => raw ? Just(raw) : Nothing())
  .then(row => {
    (row instanceof Maybe) === true;
  });

Without going into what Just and Nothing mean, the implication of this (because of how I've written Maybe ) is that the row variable in the above code has a function on it called "then" . 无需深入探讨Just and Nothing含义,其含义(因为我写Maybe )是上面代码中的row变量具有一个名为“ then”的函数 To cut a long story short, it appears that Javascript is getting confused and is for some reason automatically calling MY "then" and instead of passing the Maybe is actually passing to the callback whatever MY "then" returns as the value of row . 简而言之,似乎Javascript变得混乱,由于某种原因,它会自动调用MY“ then”,而不是传递Maybe实际上是将MY“ then”作为row值返回的内容传递给回调。 This is obviously leading to all manner of weird behaviour. 显然,这导致了各种奇怪的行为。 If I simply remove the "then" function from my object then it all works as expected. 如果我只是从对象中删除“ then”功能,那么所有功能都将按预期工作。

I am aware that if a Promise.then returns another Promise, then execution will pause until that promise is resolved. 我知道,如果Promise.then返回另一个Promise,那么执行将暂停,直到该Promise被解决。 I have been unable to find any official documentation to back this up, but is it the case that this decision is simply based on the existence of a "then" function (the closest I have found is this https://developers.google.com/web/fundamentals/primers/promises which refers to the return value as "something Promise-like"). 我一直找不到任何官方文档来支持此操作,但是是否此决定仅基于“ then”函数的存在(我发现的最接近的是https://developers.google)。 com / web / fundamentals / primers / promises ,将返回值称为“类似Promise的东西”)。 If this is the case, it would be my understanding that "then" as a function name is basically a reserved word in Javascript? 如果是这样,我的理解是“ then”作为函数名称基本上是Javascript中的保留字吗? I have seen other implementations of Maybe (such as this one https://www.npmjs.com/package/data.maybe ) that use the word "chain" for a similar thing - I wondered if this is why? 我看到过Maybe的其他实现(例如https://www.npmjs.com/package/data.maybe ),它们使用“ chain”一词来表示类似的事情-我想知道这是为什么吗?

Can anyone shed any light on if my deduction here is correct and if so is there any workaround I can use other than renaming my function? 如果我的推论是正确的,是否有人可以透露任何信息,如果可以,除了重命名我的功能之外,还有其他解决方法吗?

FYI the only other SO question I've found that touches this problem is this one - Resolve promise with an object with a "then" function - but since that is angular-specific I don't believe this is a duplication. 仅供参考,我发现碰到这个问题的唯一另一个SO问题就是这个问题- 使用具有“ then”功能的对象来解决诺言 -但这是特定于角度的,所以我不认为这是重复的。

Thanks in advance! 提前致谢!

...the row variable in the above code has a function on it called "then". ...上面代码中的row变量具有一个称为“ then”的函数。 To cut a long story short, it appears that Javascript is getting confused and is for some reason automatically calling MY "then"... 长话短说,似乎Javascript变得混乱,由于某种原因自动将MY称为“ then” ...

It's not confused. 不会混淆。 :-) This is the definition of how promises work. :-)这是承诺如何工作的定义。 JavaScript's promises work according to the Promises/A+ specification , which uses this terminology: JavaScript的promise 遵循Promises / A +规范 ,该规范使用以下术语:

1.1 “promise” is an object or function with a then method whose behavior conforms to this specification. 1.1“ promise”是具有then方法的对象或函数,其行为符合此规范。

1.2 “thenable” is an object or function that defines a then method. 1.2“ thenable”是定义then方法的对象或函数。

If you have an object passing through a promise chain that's a thenable but not a promise , it's incompatible with promises. 如果您的对象通过了可以实现但不是承诺的承诺链,则它与承诺不兼容。

So yes, in a sense, the then property of objects passing through promise chains is "reserved" by the Promises/A+ spec. 因此,是的,在某种意义上,通过Promise / A +规范“保留”了通过承诺链传递的对象的then属性。 You'll need to wrap your raw value in an object that doesn't have a then (and then unwrap it later). 您需要将raw值包装在没有then的对象中(然后再将其解包)。 Or if you can, rename then in your design to remove the conflict. 或者,如果可以的then ,请在设计中重命名以消除冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM