Search code examples
javascriptpromisecode-readability

Clear function name that indicates it will return a Promise (JavaScript)


I wanted to know if there was a naming convention that I should follow for functions that return a promise that clearly explained that .then() should be added if you expect to have the promise work.

I modified an old function addFilterForLanguage(lang) to return a promise for downloading that language's filter. In the past, it wasn't an asynchronous task, and filter would be called before it had arrived. I want a better name to clearly indicate that .then() is necessary. We often have new interns (one of which wrote the original code) who might not know about promises, and often assume that it would/will "just work."

Is there a convention for this yet, or is addFilterForLanguage_ButDontForgetToCall_Then_AndPassInAFunctionIfYouWantItToRunToMakeItSyncronous(lang) the best I can do?

(I thought addFilterForLanguage(lang).then(myFunction) was clear enough, but you never know.)


Solution

  • The Bluebird Promise library adds Async as a suffix to a function name when you use Promise.promisifyAll on a Node-style callback function.

    For instance, transforming fs.exists with promisifyAll will turn it into fs.existsAsync.

    We've used that convention in any code where Promises need to interact with Node-style callbacks.