Angular-styleguide: Thoughts on using the *Async suffix for any functions that return a promise?

Created on 7 May 2015  ·  4Comments  ·  Source: johnpapa/angular-styleguide

One thing I like to do in my JavaScript code is to add the word "Async" to the end of function names that return a promise. This improves readability and lets people know at a glance that it returns a promise when intellisense is showing you the function name.

Example:

function getDataFromThesServer() {}

Would be:

function getDataFromThesServerAsync() {}
question

All 4 comments

That is the standard at Microsoft for .NET libraries. But I think they need it and we don't in JavaScript.

They need it because they have a boat load of synchronous methods with async alternatives. The suffix helps them convey the similarity of intent without inventing a new method name root.

But it smacks of Hungarian notation which became passe when editors/IDEs began providing API help.

More to the point, we don't have the same potential for competing async and sync signatures. It happens of course, but not as often because all cross-process communications in JS world have to be async; there are no competing sync alternatives. Therefore, I think we can do without the extra writing and reading.

Moreover, it's pretty obvious looking at a method in use whether it is async or not.

You may well disagree. But I think the style guide should only recommend proven practices that are widely used and widely accepted ... which this one is not.

The style guide shouldn't prohibit it either which leaves you free to add it to your group's standards if you wish.

Here is a boring practical note. I have found that, with increasing depth of understanding, a greater percentage of all functions return promises. Adding the word Async to a significant fraction of all function in an application does not seem like a victory.

interesting discussion. to be clear, i dont do this and am not changing ... but I don't think its bad to do it. why? its a convention and if you think it reads better to you, give it a shot. but I bet after you get more and more code with this you may see what @kylecordes mentioned ... a lot of the fn's have it and you really dont need it.

I wont do it and I agree that its more common to see functions use promises than not, and practically everything is async :)

I'm also from .Net and I guess another reason of adding *Async to async functions is to let the caller know that it's an async function and don't forget to use await.

Any good practice (or IDE support ) that can remind the caller that an await might be missing?

I'm using VSCode and WebStorm.

Was this page helpful?
0 / 5 - 0 ratings