67

I feel it would be useful to have a naming convention for JavaScript variables which hold a promise. I don't generally like or advocate naming conventions beyond programming language standards, but in the style of programming where promises are passed around as function arguments it's often hard to tell at a glance whether a variable holds a promise or the "real thing".

I've personally used promiseOfFoo and pFoo, but I find the former a bit verbose, and the latter gives me flashbacks from Hungarian.

Is there a commonly used convention?

Klesun
  • 12,280
  • 5
  • 59
  • 52
jevakallio
  • 35,324
  • 3
  • 105
  • 112
  • 1
    what about `fooPromise`? – jbabey Jan 10 '13 at 21:13
  • 25
    what about `Froomise`? – elclanrs Jan 10 '13 at 21:13
  • As added justification for my vote to close: while there certainly are conventions, this question is very likely going to lead to a lot of discussion on pros and cons of the different uses of words and abbreviations, and as such it doesn't have a *single* factual answer. But I still find it a good question (just maybe not for SO), so I was torn. Which is why, +1 as well. – J. Steen Jan 11 '13 at 09:40
  • @JSteen, I was aware of the problems of the question vis the SO format, but I was hoping there would've been, if not single factual, at least a hegemonic answer. Thanks for the explanation, though. – jevakallio Jan 11 '13 at 10:10
  • This Tuts article advises using verbs in `-ing` for variables storing promises, indicating an on-going process: http://msdn.microsoft.com/en-us/library/windows/apps/hh464924.aspx – ejoubaud Oct 22 '13 at 10:33
  • 1
    This doc about Asynchronous Programming for Windows Store apps states that the convention for JS functions returning a promise is to be suffixed with `Async`: http://msdn.microsoft.com/en-us/library/windows/apps/hh464924.aspx – ejoubaud Oct 22 '13 at 10:36
  • I prefer to prefix with single letters but only for primitive types (returned by `typeof`), example `fFooPromise`, `fPromiseOfFoo`, `fPromisedFoo`. So far I find the primitive type prefixing works well across languages, however my naming convention is always evolving. – Daniel Sokolowski Jun 19 '14 at 18:39
  • 4
    This question should not be closed. The question isn't 'which is the best convention', the question is 'is there a convention'. As soon as, say, the Google JavaScript Style Guide covers this, the factual answer will be 'yes'. – Michael Scheper Jan 12 '15 at 23:01
  • @jevakallio I have written a very focused article talking specifically about this issue explaining why this is important and the confusions a wrong naming can cause: https://medium.com/@fagnerbrack/promises-are-not-proxies-fd00751eb980 – Fagner Brack Mar 19 '16 at 06:21
  • What about `eventualFoo`? Then: `foo = await eventualFoo` (isn't it proper Hungarian notation, btw)? – lajarre Aug 30 '17 at 09:23
  • [Here](https://github.com/airbnb/javascript/issues/848) man named @chardskarth provides good reasons to prefix promise variables with `when`. I love how this question is closed and I can't even reference this existing convention in an answer. Continue promoting people to flood in comments, close-voters, you're doing a great job! – Klesun Jun 11 '20 at 09:17

2 Answers2

22

This depends more on how you're going to use them, doesn't it?

If your code looks like:

var imageLoading = loadImage(url); // returns promise
imageLoading.done(showImage);

// imageLoading.done
// imageLoading.error
// imageLoading.then
// imageLoading.success
// imageLoading.fail
// ... whatever your library supports

Then, I might suggest naming the promise something like a present-tense verb...

BUT if you're building a library which depends on deferred objects

// accepts a promise
var showImage = function (promise) {
    promise.done(function (img) { /* ...... */ });
};

Then there's nothing particularly wrong with naming the variable as a noun, so long as there's an understanding as to which methods take promises and which don't.

var image = loadImage(url); // returns promise
showImage(image);           // acts on promise

Now your interfaces are really clean, and you can write code which looks 100% procedural. ...buuuut, you need to know which functions/methods use promises and which use objects.

If you are passing promises as callbacks, inside of object methods, then you can happily name them promise or tweetLoading or dataParsing or whatever makes sense within the context of that particular situation.

For the definition of showImage, the parameter I chose is flat-out called promise, so that if you're doing work on that function, or you needed to debug a chain of stuff, you could see the second you looked at it that it took a promise object.

Norguard
  • 26,167
  • 5
  • 41
  • 49
  • 2
    +1. I like the present participle idea ("verb-ing"), though it would only be applicable if the **action** is imporant to the meaning of the promise (like `dataParsing`). Often promises are about the end result (data), so in those cases not so much. I also like the idea naming a function parameter just `promise` and inferring the meaning from context. Good ideas, but no conventions. – jevakallio Jan 10 '13 at 22:04
  • @fencliff promises are frequently about the end-result, but the library-APIs for subscription to the promise all focus around the action `tweet.done(callback)` versus `loading.done(callback)`, as deferring a value's availability is dependent upon some sort of action (even if it's just polling after a time). That's pretty much universal. In terms of conventions, I really don't see one. Write what reads well, rather than enforcing new-Hungarian, or adopting a `_00_p` prefix/suffix as a standard convention. More people will be liable to understand `loading.finished();` than `prefix_X.done();` – Norguard Jan 10 '13 at 22:11
  • If you think about it, by naming with a present participle ('loading', 'fetching', 'saving') you are effectively using a suffix convention ("ing") as opposed to a prefix convention ("p_" or whatever). No major difference, except arguably a degree of readbility for English speakers. Many other languages don't have, or don't exploit, present participles so I'm not sure this makes a good convention for worldwide use. – Beetroot-Beetroot Jan 11 '13 at 01:00
  • 1
    @Beetroot-Beetroot I agree that it's an English convention, used only by people who speak English as their [1-n]+suffix language. However, JavaScript's keywords (and otherwise reserved words) are also unfit for globalized use, as the abstract understanding of `else` or `continue` doesn't really translate into Urdu, either. I get where you're coming from, and yes, I'm being pedantic, but if we were going to do away with linguistic conventions, why don't we just go back to bit-arithmetic in assembly? A "promise" a "future" a "deferred" all do similar things, and are all English-based paradigms. – Norguard Jan 11 '13 at 03:18
  • @Norguard, it is certainly true that jQuery and programming languages in general are inextricably wedded to the English language, however it is evident that programmers whose first language is not English tend to name their variables using words/contructs from their first language. Unlike programming language natives, this is an aspect in which all of us can exercise choice. – Beetroot-Beetroot Jan 11 '13 at 03:37
  • @Beetroot-Beetroot but here, we end up at the crux; people will write what makes sense in their own language. A French programmer is unlikely to be working on code written in Greek. Hungarian-notation might make sense when everybody has `int`, `float`, `double`, `string`, etc, but what is a universal, strongly-typed `promise`? And is it a `promise` a `deferred`, a `future`, etc, because different languages/libraries have different constructs, and I usually write my own, anyway... So now we're back to languages and constructs having meaning in their own language, so write what reads well. – Norguard Jan 11 '13 at 05:23
0

I don't know of a public convention but have used the following in my own code :

  • var dfrd: a Deferred object (I've don't recall ever needing two or more in the same scope)
  • var p: a Promise
  • var p_foo: one of several named Promises
  • var promises: an array or plain object containing Promises

The exception is a jqXHR object, which I will name var jqXHR (again, I don't recall ever needing two or more in the same scope).

Beetroot-Beetroot
  • 18,022
  • 3
  • 37
  • 44
  • 4
    Way too terse — although somewhat close to Hungarian Notation (https://en.wikipedia.org/wiki/Hungarian_notation) – Tom McKenzie Mar 17 '16 at 01:21