JavaScript naming convention for promises?

I believe it would be useful to have a naming convention for JavaScript variables that contain a promise . I usually don’t like or defend naming conventions other than programming language standards, but in a programming style where promises are passed as arguments to a function, it’s often difficult to immediately understand if a variable has a promise or β€œthe real thing.”

I personally used promiseOfFoo and pFoo , but I believe that the former is a bit detailed, and the latter gives me memories from Hungarian.

Is there a generally accepted agreement?

+43
javascript promise naming-conventions deferred
Jan 10 '13 at 21:12
source share
2 answers

It depends more on how you are going to use them, right?

If your code looks like this:

 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 could offer to call the promise something like a verb of current time ...

BUT if you create a library that depends on pending objects

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

Then there is nothing particularly wrong with naming a variable as a noun if there is an understanding of which methods accept promises and which do not.

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

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

If you pass promises as callbacks inside object methods, you can happily name them promise or tweetLoading or dataParsing or something that makes sense in the context of this particular situation.

To determine showImage parameter I that I selected is flat, called promise , so if you are working on this function or you need to debug a chain of things, you can see, secondly, you looked at it that it received a promise object.

+15
Jan 10 '13 at 21:53
source share

I do not know the public agreement, but used my code:

  • var dfrd : deferred object (I don't remember ever needing two or more in the same area)
  • var p : promise
  • var p_foo : one of several named Promises
  • var promises : an array or simple object containing Promises

The exception is the jqXHR object, which I will call var jqXHR (again, I don’t remember ever needing two or more in the same area).

0
Jan 10 '13 at
source share



All Articles