Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a function that returns a promise called?

What do you call a function which returns a promise?

This is not the start of a joke

like image 646
Ross Avatar asked May 11 '15 16:05

Ross


2 Answers

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.

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.

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.

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.

like image 104
Christian Davis Avatar answered Nov 16 '22 23:11

Christian Davis


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.

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).

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

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.

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.
  • "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.
  • "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).

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.

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 :)

like image 33
Benjamin Gruenbaum Avatar answered Nov 17 '22 00:11

Benjamin Gruenbaum