async function
should have await
expression JS-0116async
function without any await
expressions 52 });
53}
54
55export async function asyncHttp<T = SuccessResponse>(request: Request) { 56 return fetch(request) 57 .then((r) => r.json()) 58 .then((data) => { 59 if (Object.prototype.hasOwnProperty.call(data, "dd")) { 60 window.dispatchEvent(new CustomEvent("__dd", { detail: data.dd })); 61 } else if (Object.prototype.hasOwnProperty.call(data, "whoops")) { 62 window.dispatchEvent( 63 new CustomEvent("__whoops", { detail: data.whoops }) 64 ); 65 } else if (Object.prototype.hasOwnProperty.call(data, "redirect")) { 66 document.location = data.redirect; 67 } 68 return data as T; 69 }); 70} 71
72export function httpGet<T = SuccessResponse>(
73 url: string,
async
function without any await
expressions109 http<T>(r, callback);
110}
111
112export async function asyncHttpGet<T = SuccessResponse>(url: string) {113 const r = new Request(url, {114 method: "GET",115 headers: {116 "X-Requested-With": "XMLHttpRequest",117 },118 });119 return asyncHttp<T>(r);120}121
122export async function asyncHttpPost<T = SuccessResponse>(
123 url: string,
async
function without any await
expressions119 return asyncHttp<T>(r);
120}
121
122export async function asyncHttpPost<T = SuccessResponse>(123 url: string,124 data: Record<string, unknown>125) {126 const r = new Request(url, {127 method: "POST",128 headers: {129 "Content-Type": "application/json",130 "X-Requested-With": "XMLHttpRequest",131 },132 body: JSON.stringify(data),133 });134 return asyncHttp<T>(r);135}
A function that does not contain any await
expressions should not be async
(except for some edge cases
in TypeScript which are discussed below).
Asynchronous functions in JavaScript behave differently than other functions in two important ways:
Promise
.await
operator inside them.Functions are made async
so that we can use the await
operator inside them.
Consider this example:
async function fetchData(processDataItem) {
const response = await fetch(DATA_URL);
const data = await response.json();
return data.map(processDataItem);
}
Asynchronous functions that don't use await
might be an unintentional result of refactoring.
Note: This issue ignores async generator functions.
Generators yield
rather than return
a value and async
generators might yield all the values of another async generator without ever actually needing to use await
.
In TypeScript, one might feel the need to make a function async
to comply with type signatures defined by an interface.
Ideally, the code should be refactored to get rid of such restrictions, but sometimes that isn't feasible
(For example, when we are implementing an interface defined in a 3rd party library like Next.js).
This situation can easily be circumvented by returning the value with a call to Promise.resolve
:
interface HasAsyncFunc {
getNum: () => Promise<number>
}
// Not recommended:
const o: HasAsyncFunc = {
async getNum() { return 1 }
}
// Recommended:
const o: HasAsyncFunc = {
// We only use `Promise.resolve` to adhere to the type
// of the surrounding object.
getNum() { return Promise.resolve(1) }
}
It is also advised to add a comment near the redundant promise to make the intent clear.
async function fetchData(): string {
// `readFileSync` is a synchronous function that blocks
// the main thread, and thus does not need to be `await`ed
return fs.readFileSync("data.txt", "utf-8");
}
performAction(async () => { console.log("no awaits in here") });
async function fetchDataAsync(): Promise<string> {
return await fs.readFile("data.txt", "utf-8")
}
performAction(async () => { await writeToFile(data) });
// Allow empty functions.
async function no_op() {}