什么是返回所谓的承诺的函数?

时间:2022-06-15 19:37:11

What do you call a function which returns a promise?

你怎么称呼一个返回承诺的函数?

This is not the start of a joke

这不是笑话的开始

5 个解决方案

#1


In the javascript community I've seen a function that returns a promise called being "thenable" enough times that I think "thenable" when I'm coding. It's a thenable function.

在javascript社区中,我看到一个函数返回一个名为“thenable”的promise,足以让我在编码时觉得“可以”。这是一个很好的功能。

I like this name for a number of reasons:

我喜欢这个名字有很多原因:

1) Thenable describes the functional behaviour. If it returns a promise, you can call "then" on it.

1)然后描述功能行为。如果它返回一个promise,你可以在它上面调用“then”。

2) It's unique. Promises aren't exactly monadic, so monad isn't appropriate. "Async" is more of a super class of functions and doesn't help describe the nature of a Promise. But "thenable" is uniquely Promisely.

2)它是独一无二的。承诺不完全是monadic,所以monad不合适。 “异步”更像是一类超级函数,并没有帮助描述Promise的本质。但“可怜”是唯一的Promisely。

3) I also like that it's not repeating Promise everywhere, (Promissory, Promisified, Promise Function, etc.) which is approaching a circular definition IMO. Being able to google one word which has one specific meaning is really nice.

3)我也喜欢它并不重复无处不在的承诺,(承诺,承诺,承诺功能等)正在接近IMO的循环定义。能够谷歌一个具有一个特定含义的单词真的很好。

#2


Well...

The current answers aren't really correct. The truth of the matter is there is no short clever name for "function that returns a promise" that is in the consensus of the JS community.

目前的答案并不正确。问题的真相是,JS社区的共识中没有“返回承诺的功能”的简短名称。

In JavaScript

The spec doesn't name them in a special way, no documentation of any popular library names them in a special way. The promises specification does not name them in any special way.

该规范没有以特殊方式命名它们,没有任何流行库的文档以特殊方式命名它们。承诺规范没有以任何特殊方式命名它们。

The origin of promises

From the other hand, if we check the literature branch originating from Liskov and Shrira, they don't, and neither do Bogle nor Zondervan use any term other than "getting a promise" or "getting the future" for it (they do use claim for extracting the value).

另一方面,如果我们检查来自Liskov和Shrira的文献分支,他们不会,Bogle和Zondervan也不会使用除“获得承诺”或“获得未来”之外的任何术语(他们确实使用要求提取价值)。

If we check Mark Miller's work he does not use any terms for it either.

如果我们查看Mark Miller的作品,他也不会使用任何条款。

Other languages

In other languages things don't fare better. C# has no special name for methods that return Tasks (its promises). It does have "async functions" for functions that return async/await but that's only a strict subset. No special name in Scala, no special name in Python, no special name in Java and so on. The only thing close was parse's Android API naming their promises' then callbacks Continuation but that's hardly meaningful in this context.

在其他语言中,事情并不好。对于返回Tasks(其承诺)的方法,C#没有特殊名称。对于返回async / await的函数,它确实有“异步函数”,但这只是一个严格的子集。 Scala中没有特殊名称,Python中没有特殊名称,Java中没有特殊名称,等等。唯一关闭的是parse的Android API命名他们的承诺'然后回调延续,但在这种情况下这几乎没有意义。

Name suggestions

If it makes you feel any better - we don't have a name for something that returns an array either.

如果它让你感觉更好 - 我们没有返回数组的名称。

  • "Monadic functions" - Lots of functions can return monads that are not promises, moreover promises aren't really monads - they don't actually conform in their current form to monad laws nor does then make a valid bind. More importantly, very few JS developers even know what monads are and even fewer care.
  • “Monadic函数” - 许多函数可以返回不是promises的monad,而且promises不是真正的monad - 它们实际上并不符合monad定律,也没有做出有效的绑定。更重要的是,很少有JS开发人员甚至知道monad是什么,甚至更少关心。

  • "async function" - this might hold when (and if! it's not 100% sure now) the async modifier is added to the language. Right now and probably event then a lot of people consider things like setTimeout or fs.openFile to be async functions.
  • “异步函数” - 这可能会保留(如果现在不是100%确定)async修饰符被添加到语言中。现在,可能是事件,然后很多人认为像setTimeout或fs.openFile这样的东西是异步函数。

  • "Kleisli Arrows" - I took a type theory course and it covered this topic and I don't think me or any other students would call a function that returns a monad a Kleisli arrow. For one thing because it's an Arrow (yes yes, arrows are functions I get it).
  • “Kleisli Arrows” - 我参加了一门类型理论课程,它涵盖了这个主题,我认为我或任何其他学生都不会称一个函数返回一个单一的Kleisli箭头。首先,因为它是一个箭头(是的,箭头是我得到的功能)。

Conclusion

To conclude, the people who invented promises, the people who specified them for JavaScript, the people who put that spec in writing, the spec itself, the promise libraries, the libraries written with those and the average user don't have any special name for it.

总而言之,发明承诺的人,为JavaScript指定它​​们的人,编写该规范的人,规范本身,承诺库,用这些编写的库和普通用户都没有任何特殊名称为了它。

I think we can stick to A function that returns a promise.

我认为我们可以坚持一个返回承诺的函数。

If it makes you feel any better - we don't name "functions that return an int" either :)

如果它让你感觉更好 - 我们不会命名“返回int的函数”:)

#3


We call them asynchronous functions. Even if the function does nothing asynchronous itself, it at least returns a promise - an asynchronous value.

我们称它们为异步函数。即使函数本身没有异步,它至少会返回一个promise - 一个异步值。

You can shorten that to async function, although that term is also used for the ES7 syntactic notation async function that creates functions which return promises implicitly (whose code can use await).

您可以将其缩短为异步函数,尽管该术语也用于ES7语法表示法异步函数,该函数创建隐式返回promise的函数(其代码可以使用等待)。

If you want to think of the type of functions that return monads (which promises are), you can call them Kleisli arrows. But you did ask a [javascript] question, not a [type-theory] one, did you?

如果你想想返回monad(承诺是什么)的函数类型,你可以称它们为Kleisli箭头。但你确实问了一个[javascript]问题,而不是[类型理论],你呢?

#4


In my experience, there are no individual words to describe functions that return a particular type. They are generally called "a function that returns a <Type>".

根据我的经验,没有单独的词来描述返回特定类型的函数。它们通常被称为“返回 ”的函数。

For example:

  • foo() is a function that returns a String
  • foo()是一个返回String的函数

  • bar() is a function that returns a Number
  • bar()是一个返回Number的函数

  • baz() is a function that returns a Promise
  • baz()是一个返回Promise的函数

In this context, Promise is being used similar to how an interface would in a strongly typed language.

在这种情况下,Promise的使用类似于接口在强类型语言中的使用方式。

Another example of a generic type being used would be

使用的通用类型的另一个例子是

  • fizz() is a function that returns a collection
  • fizz()是一个返回集合的函数


That's not to say that there can't be a word with a definition of "a function that returns a Promise", but there's no commonly used jargon to describe this.

这并不是说没有一个单词带有“返回Promise的函数”的定义,但是没有常用的术语来描述它。

"deferred function" is sometimes used to describe a function that returns promises or promise-like objects (i.e. jQuery's Deferred Object).

“延迟函数”有时用于描述返回promises或类似promise的对象(即jQuery的Deferred Object)的函数。

Functions that return promises can also be described by other more general words (in much the same way that a square can be described as a rectangle, parallelogram, rhombus, or quadrilateral).

返回promise的函数也可以用其他更通用的单词来描述(与正方形可以描述为矩形,平行四边形,菱形或四边形的方式大致相同)。

  • Promises are monads, so they can be described as "monadic".
  • 承诺是单子,所以它们可以被描述为“monadic”。

  • Promises are asynchronous, so functions that utilize them can be described as asynchronous as well.
  • Promise是异步的,因此利用它们的函数也可以描述为异步。


Personally, I'd go with "promissory functions", as "promissory" means:

就个人而言,我会选择“承诺函数”,因为“承诺”意味着:

conveying or implying a promise

传达或暗示承诺

and I like the idea that the return value of such a function is "conveying a promise".

我喜欢这样一个功能的返回值是“传达承诺”的想法。

#5


Technically, a promise is a monad. Hence, a function that returns a promise is a monadic function.

从技术上讲,承诺是一个单子。因此,返回promise的函数是monadic函数。

#1


In the javascript community I've seen a function that returns a promise called being "thenable" enough times that I think "thenable" when I'm coding. It's a thenable function.

在javascript社区中,我看到一个函数返回一个名为“thenable”的promise,足以让我在编码时觉得“可以”。这是一个很好的功能。

I like this name for a number of reasons:

我喜欢这个名字有很多原因:

1) Thenable describes the functional behaviour. If it returns a promise, you can call "then" on it.

1)然后描述功能行为。如果它返回一个promise,你可以在它上面调用“then”。

2) It's unique. Promises aren't exactly monadic, so monad isn't appropriate. "Async" is more of a super class of functions and doesn't help describe the nature of a Promise. But "thenable" is uniquely Promisely.

2)它是独一无二的。承诺不完全是monadic,所以monad不合适。 “异步”更像是一类超级函数,并没有帮助描述Promise的本质。但“可怜”是唯一的Promisely。

3) I also like that it's not repeating Promise everywhere, (Promissory, Promisified, Promise Function, etc.) which is approaching a circular definition IMO. Being able to google one word which has one specific meaning is really nice.

3)我也喜欢它并不重复无处不在的承诺,(承诺,承诺,承诺功能等)正在接近IMO的循环定义。能够谷歌一个具有一个特定含义的单词真的很好。

#2


Well...

The current answers aren't really correct. The truth of the matter is there is no short clever name for "function that returns a promise" that is in the consensus of the JS community.

目前的答案并不正确。问题的真相是,JS社区的共识中没有“返回承诺的功能”的简短名称。

In JavaScript

The spec doesn't name them in a special way, no documentation of any popular library names them in a special way. The promises specification does not name them in any special way.

该规范没有以特殊方式命名它们,没有任何流行库的文档以特殊方式命名它们。承诺规范没有以任何特殊方式命名它们。

The origin of promises

From the other hand, if we check the literature branch originating from Liskov and Shrira, they don't, and neither do Bogle nor Zondervan use any term other than "getting a promise" or "getting the future" for it (they do use claim for extracting the value).

另一方面,如果我们检查来自Liskov和Shrira的文献分支,他们不会,Bogle和Zondervan也不会使用除“获得承诺”或“获得未来”之外的任何术语(他们确实使用要求提取价值)。

If we check Mark Miller's work he does not use any terms for it either.

如果我们查看Mark Miller的作品,他也不会使用任何条款。

Other languages

In other languages things don't fare better. C# has no special name for methods that return Tasks (its promises). It does have "async functions" for functions that return async/await but that's only a strict subset. No special name in Scala, no special name in Python, no special name in Java and so on. The only thing close was parse's Android API naming their promises' then callbacks Continuation but that's hardly meaningful in this context.

在其他语言中,事情并不好。对于返回Tasks(其承诺)的方法,C#没有特殊名称。对于返回async / await的函数,它确实有“异步函数”,但这只是一个严格的子集。 Scala中没有特殊名称,Python中没有特殊名称,Java中没有特殊名称,等等。唯一关闭的是parse的Android API命名他们的承诺'然后回调延续,但在这种情况下这几乎没有意义。

Name suggestions

If it makes you feel any better - we don't have a name for something that returns an array either.

如果它让你感觉更好 - 我们没有返回数组的名称。

  • "Monadic functions" - Lots of functions can return monads that are not promises, moreover promises aren't really monads - they don't actually conform in their current form to monad laws nor does then make a valid bind. More importantly, very few JS developers even know what monads are and even fewer care.
  • “Monadic函数” - 许多函数可以返回不是promises的monad,而且promises不是真正的monad - 它们实际上并不符合monad定律,也没有做出有效的绑定。更重要的是,很少有JS开发人员甚至知道monad是什么,甚至更少关心。

  • "async function" - this might hold when (and if! it's not 100% sure now) the async modifier is added to the language. Right now and probably event then a lot of people consider things like setTimeout or fs.openFile to be async functions.
  • “异步函数” - 这可能会保留(如果现在不是100%确定)async修饰符被添加到语言中。现在,可能是事件,然后很多人认为像setTimeout或fs.openFile这样的东西是异步函数。

  • "Kleisli Arrows" - I took a type theory course and it covered this topic and I don't think me or any other students would call a function that returns a monad a Kleisli arrow. For one thing because it's an Arrow (yes yes, arrows are functions I get it).
  • “Kleisli Arrows” - 我参加了一门类型理论课程,它涵盖了这个主题,我认为我或任何其他学生都不会称一个函数返回一个单一的Kleisli箭头。首先,因为它是一个箭头(是的,箭头是我得到的功能)。

Conclusion

To conclude, the people who invented promises, the people who specified them for JavaScript, the people who put that spec in writing, the spec itself, the promise libraries, the libraries written with those and the average user don't have any special name for it.

总而言之,发明承诺的人,为JavaScript指定它​​们的人,编写该规范的人,规范本身,承诺库,用这些编写的库和普通用户都没有任何特殊名称为了它。

I think we can stick to A function that returns a promise.

我认为我们可以坚持一个返回承诺的函数。

If it makes you feel any better - we don't name "functions that return an int" either :)

如果它让你感觉更好 - 我们不会命名“返回int的函数”:)

#3


We call them asynchronous functions. Even if the function does nothing asynchronous itself, it at least returns a promise - an asynchronous value.

我们称它们为异步函数。即使函数本身没有异步,它至少会返回一个promise - 一个异步值。

You can shorten that to async function, although that term is also used for the ES7 syntactic notation async function that creates functions which return promises implicitly (whose code can use await).

您可以将其缩短为异步函数,尽管该术语也用于ES7语法表示法异步函数,该函数创建隐式返回promise的函数(其代码可以使用等待)。

If you want to think of the type of functions that return monads (which promises are), you can call them Kleisli arrows. But you did ask a [javascript] question, not a [type-theory] one, did you?

如果你想想返回monad(承诺是什么)的函数类型,你可以称它们为Kleisli箭头。但你确实问了一个[javascript]问题,而不是[类型理论],你呢?

#4


In my experience, there are no individual words to describe functions that return a particular type. They are generally called "a function that returns a <Type>".

根据我的经验,没有单独的词来描述返回特定类型的函数。它们通常被称为“返回 ”的函数。

For example:

  • foo() is a function that returns a String
  • foo()是一个返回String的函数

  • bar() is a function that returns a Number
  • bar()是一个返回Number的函数

  • baz() is a function that returns a Promise
  • baz()是一个返回Promise的函数

In this context, Promise is being used similar to how an interface would in a strongly typed language.

在这种情况下,Promise的使用类似于接口在强类型语言中的使用方式。

Another example of a generic type being used would be

使用的通用类型的另一个例子是

  • fizz() is a function that returns a collection
  • fizz()是一个返回集合的函数


That's not to say that there can't be a word with a definition of "a function that returns a Promise", but there's no commonly used jargon to describe this.

这并不是说没有一个单词带有“返回Promise的函数”的定义,但是没有常用的术语来描述它。

"deferred function" is sometimes used to describe a function that returns promises or promise-like objects (i.e. jQuery's Deferred Object).

“延迟函数”有时用于描述返回promises或类似promise的对象(即jQuery的Deferred Object)的函数。

Functions that return promises can also be described by other more general words (in much the same way that a square can be described as a rectangle, parallelogram, rhombus, or quadrilateral).

返回promise的函数也可以用其他更通用的单词来描述(与正方形可以描述为矩形,平行四边形,菱形或四边形的方式大致相同)。

  • Promises are monads, so they can be described as "monadic".
  • 承诺是单子,所以它们可以被描述为“monadic”。

  • Promises are asynchronous, so functions that utilize them can be described as asynchronous as well.
  • Promise是异步的,因此利用它们的函数也可以描述为异步。


Personally, I'd go with "promissory functions", as "promissory" means:

就个人而言,我会选择“承诺函数”,因为“承诺”意味着:

conveying or implying a promise

传达或暗示承诺

and I like the idea that the return value of such a function is "conveying a promise".

我喜欢这样一个功能的返回值是“传达承诺”的想法。

#5


Technically, a promise is a monad. Hence, a function that returns a promise is a monadic function.

从技术上讲,承诺是一个单子。因此,返回promise的函数是monadic函数。