Add "async" keyword to asynchronous functions that return a promise
See original GitHub issueDescription
In order to make it easier for developers to identify asynchronous methods, the following conventions need to be introduced:
- Use of “async” keyword in the function definitions.
- 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?
- Search for methods that are then-able. You can do a simple search for
.then(
to reveal asynchronous then-able methods. - 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 isPromise<ClassroomData
, which means that the function returns a Promise object that resolves with a value of typeClassroomData
. As long as the return type is of the formPromise<..>
, the function can be changed to follow the convention mentioned in the description. - 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
Steps to follow
- On your local setup, add the eslint check
@typescript-eslint/promise-function-async
(see this). - 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.
- 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
- 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.
- 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.
- 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:
- Created 3 years ago
- Comments:200 (141 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@vojtechjelinek please assign me
@kevintab95 #12302, #12291 and #12151 are merged. Can you please update the description? Thanks 😃