async function
should have await
expression JS-0116async
function without any await
expressions237 )
238}
239
240export const getStaticPaths: GetStaticPaths = async () => {241 return {242 paths: [],243 fallback: true,244 }245 // }246}247
248export const getStaticProps: GetStaticProps<
249 { slug: string; organizationId: string },
async
function without any await
expressions172 )
173}
174
175export const getStaticPaths: GetStaticPaths = async () => {176 return {177 paths: [],178 fallback: true,179 }180}181export const getStaticProps: GetStaticProps<
182 Record<string, unknown>,
183 RoutedQuery<'/org/[slug]/[orgLocationId]'>
async
function without any await
expressions146 </>
147 )
148}
149export const getStaticPaths: GetStaticPaths = async () => {150 return {151 paths: [],152 fallback: 'blocking',153 }154}155export const getStaticProps: GetStaticProps<
156 Record<string, unknown>,
157 RoutedQuery<'/search/intl/[country]'>
async
function without any await
expressions 1import type OtaClient from '@crowdin/ota-client'
2
3export const createCommonFns = ({ common, database }: { common: OtaClient; database: OtaClient }) => ({
4 fetchCrowdinFile: async (file: string, lang: string) => { 5 common.setCurrentLocale(lang) 6 return common.getFileTranslations(file) 7 }, 8 fetchCrowdinDbKey: async (ns: string, lang: string) => ({
9 [ns]: await database.getStringByKey(ns, lang),
10 }),
async
function without any await
expressions 5
6import { type TGetParentNameSchema } from './query.getParentName.schema'
7
8const getParentName = async ({ input }: TRPCHandlerParams<TGetParentNameSchema>) => { 9 const { slug, orgLocationId } = input1011 switch (true) {12 case Boolean(slug): {13 return prisma.organization.findUniqueOrThrow({14 where: { slug },15 select: { name: true },16 })17 }18 case Boolean(orgLocationId): {19 return prisma.orgLocation.findUniqueOrThrow({20 where: { id: orgLocationId },21 select: { name: true },22 })23 }24 default: {25 throw new TRPCError({ code: 'BAD_REQUEST' })26 }27 }28}29export default getParentName
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() {}