question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add "async" keyword to asynchronous functions that return a promise

See original GitHub issue

Description

In order to make it easier for developers to identify asynchronous methods, the following conventions need to be introduced:

  1. Use of “async” keyword in the function definitions.
  2. Use of “Async” added to the end of the function name.

E.g.

Before changes:

fetchClassroomData(classroomUrlFragment: string): Promise<ClassroomData> {
  return new Promise((resolve, reject) => {
    // some asynchronous operation.
  });
}

After changes:

async fetchClassroomDataAsync(classroomUrlFragment: string): Promise<ClassroomData> {
  return new Promise((resolve, reject) => {
    // some asynchronous operation.
  });
}

Note that (1) “async” keyword was added before function name and (2) fetchClassroomData has changed to fetchClassroomDataAsync (“Async” is added to the name).

For anonymous function definitions, the “async” keyword needs to be added before the “function” keyword. Example:

Before:

const getThreads = function() {
  return new Promise((resolve, reject) => {
    // some asynchronous operation.
  });
}

After:

const getThreadsAsync = async function() {
  return new Promise((resolve, reject) => {
    // some asynchronous operation.
  });
}

Tips

How to find out which functions need this change?

  1. Search for methods that are then-able. You can do a simple search for .then( to reveal asynchronous then-able methods.
  2. Go to the function definition of a function found in step 1 and check if the return type for the function from step 1 is a Promise object. For example, in the fetchClassroomDataAsync function mentioned above, the return type is Promise<ClassroomData, which means that the function returns a Promise object that resolves with a value of type ClassroomData. As long as the return type is of the form Promise<..>, the function can be changed to follow the convention mentioned in the description.
  3. If the function does not have the return type defined, the function definition needs to be updated with the appropriate return type before it can follow the convention mentioned in the description. For information on how to define types, please see “Guide on Defining Types”.

Here are some examples from the code base

https://github.com/oppia/oppia/blob/e4cd1e8c58232f04e642a6c61133e6716d59bf45/core/templates/services/state-top-answers-stats-backend-api.service.ts#L40-L49

https://github.com/oppia/oppia/blob/e4cd1e8c58232f04e642a6c61133e6716d59bf45/core/templates/services/state-top-answers-stats.service.ts#L68-L87

https://github.com/oppia/oppia/blob/e4cd1e8c58232f04e642a6c61133e6716d59bf45/core/templates/services/state-top-answers-stats.service.ts#L112-L117

https://github.com/oppia/oppia/blob/e4cd1e8c58232f04e642a6c61133e6716d59bf45/core/templates/services/exploration-improvements.service.ts#L143-L146

https://github.com/oppia/oppia/blob/e4cd1e8c58232f04e642a6c61133e6716d59bf45/core/templates/domain/classroom/classroom-backend-api.service.ts#L99-L111

Steps to follow

  1. On your local setup, add the eslint check @typescript-eslint/promise-function-async (see this).
  2. Run the lint check on the CI on your fork of Oppia or on your local machine. The linter will report all the files that require this change.
  3. Follow the instructions mentioned in the How to find out which functions need this change? section to introduce the new keyword and pattern.

Important Notes

  1. All the files listed by the eslint check needs to be handled and the PR that fully fixes this issue must also introduce the lint check.
  2. This task can be assigned and worked on only after the following PRs have been merged https://github.com/oppia/oppia/pull/12302, https://github.com/oppia/oppia/pull/12291, https://github.com/oppia/oppia/pull/12194, https://github.com/oppia/oppia/pull/12188, https://github.com/oppia/oppia/pull/12164, https://github.com/oppia/oppia/pull/12151, https://github.com/oppia/oppia/pull/12020.
  3. The assignee for this task must ensure that the async / …Async pattern is introduced across the codebase in their PR.

Note: For a guide on how to access Oppia’s webpages, see this.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:200 (141 by maintainers)

github_iconTop GitHub Comments

2reactions
nlok5923commented, Mar 11, 2021

@vojtechjelinek please assign me

 core/templates/domain/topic/topic-creation-backend-api.service.ts
 core/templates/domain/story_viewer/story-viewer-backend-api.service.ts
1reaction
aasiffaizalcommented, Mar 29, 2021

@kevintab95 #12302, #12291 and #12151 are merged. Can you please update the description? Thanks 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

async function - JavaScript - MDN Web Docs - Mozilla
The async and await keywords enable asynchronous, promise-based behavior to ... Await expressions make promise-returning functions behave as ...
Read more >
Async/await - The Modern JavaScript Tutorial
asynchronous code returns a value, but it wraps the value in a Promise, so that the caller can use it to specify the...
Read more >
Async and Await in JavaScript, the extension to a promise.
The keyword await is used to wait for a Promise. It can only be used inside an async function. This keyword makes JavaScript...
Read more >
How to return the result of an asynchronous function in ...
Find out how to return the result of an asynchronous function, promise based or callback based, using JavaScript.
Read more >
How to convert an asynchronous function to return a promise ...
Example 2: You can also add await keyword and store that result in some variable. This is helpful when we fetch data from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found