Javascript: Naming convention for Promise.

Created on 22 Apr 2016  路  7Comments  路  Source: airbnb/javascript

I'm so confused. In general case, we name the function like 'verb-what' like getItem, setQuantity, request, etc.

On case of GET functions, we can guess what type or what kind of values will return.

How about the function which return promise witch will return some other values?
Or, a just Promise. How should I name that?

For example, there are a promise which will query the user's name from database, should the promise name be getName ? or getNamePromise?

In first case(getName), other co-worker could misguess that this function will return name synchronously, so he could directly assign the variables value, like this,

const name = getName();
console.log(name) // Promise {[[PromiseStatus]]: "pending"....}

it doesn't seem good to add 'Async' suffix (getNameAsync).
adding prefix 'promise' ( promiseGetName ) neither.

promiseGettingName
gettingName
...
hmm....

Any good idea for this?

question

Most helpful comment

I was searching for something like this: A naming convention to let my code readers know instantly that the variable is a promise. How about prepending when?

let fsJetpack = require("fs-jetpack");
let whenCurrentTree = fsJetpack.inspectTreeAsync(contentsPath);

I thought of the word when because of the ff reasons:

  • it sounds like then. a promise standard word
  • in english terms, it should mean 'when this happens'

All 7 comments

Do the verbs, like 'fetch' or 'load', look like return promise?
Because when I use Fetch API, it was ok for name of 'fetch' which return promise.

if were, using 'loadName' instead 'getName' could be a solution, I think.

We haven't yet written up best practices for Promises (see #597 #216).

I'm not convinced any special naming is needed. Certainly add "Async" or "Promise" would be subpar.

But so many of .NET frameworks and libraries use 'Async' suffix so it make sure that function will return Task<> (promise) or that would need await.

How can we think function getBoo is Sync or Async?
In browser(specifically react-redux environment), the programmer might not consider about Promise's naming because there are a few Promise work against Node.js. So maybe I feel more sensitive about this problem. I'm on hard with Nodejs, Promise Things...

Generally functions that name with prefix 'get' really seem that it return some value immediately, isn't it?
I think this could make developer confused it is Async function or Sync function. Should we have to check always it is really return value immediately or promise?

JS doesn't have types - so you know what the function will return the same way as with every other function: by referring to its implementation/documentation. A convention in a codebase can certainly allow you to pre-memorize what it means, but you still have to learn its meaning in the first place.

Also, async function and Async function and "async function that takes a node-style callback" all mean that the term is confusing - at this point, async function means only a function that is syntactically guaranteed to return a Promise.

Closing this in favor of #216.

I was searching for something like this: A naming convention to let my code readers know instantly that the variable is a promise. How about prepending when?

let fsJetpack = require("fs-jetpack");
let whenCurrentTree = fsJetpack.inspectTreeAsync(contentsPath);

I thought of the word when because of the ff reasons:

  • it sounds like then. a promise standard word
  • in english terms, it should mean 'when this happens'

Now that async/await is the norm, how about prefixing with for?

const forCustomerData = async () => new Promise(...)
const customerData = await forCustomerData()
Was this page helpful?
0 / 5 - 0 ratings

Related issues

zurfyx picture zurfyx  路  3Comments

surfaceowl picture surfaceowl  路  3Comments

graingert picture graingert  路  3Comments

tpiros picture tpiros  路  3Comments

golopot picture golopot  路  3Comments