async function
should have await
expression JS-0116async
function without any await
expressions58 res.send(ret);
59};
60
61const getDefaults = async (req: Request, res: Response, next: NextFunction) => {62 console.log(req.query, req.query.mint, req.query["mint"]);63 let primitive = req.query.mint;64 let key = primitive as string;65 let technology = ComponentAPI.getComponentWithMINT(key);6667 if (technology === null) {68 console.error("Could not find MINT:", key);69 return res.status(400).send({ message: `MINT Not found - ${key}` });70 }71 console.log("Defaults:", primitive, technology.defaults);72 return res.send(technology.defaults);73};74
75export default { getDimensions, getTerminals, getDefaults };
async
function without any await
expressions27 return res.send({"x-span":ret.xspan, "y-span":ret.yspan});
28};
29
30const getTerminals = async (req: Request, res: Response, next: NextFunction) => {31 let primitive = req.query.mint;32 let key = primitive as string;33 let technology = ComponentAPI.getComponentWithMINT(key);3435 if (technology === null) {36 console.error("Could not find MINT:", key);37 return res.status(400).send({ message: `MINT Not found - ${key}` });38 }3940 let params_text = req.query.params as string;41 let params = JSON.parse(params_text);42 console.log("Params:", params);43 params["position"] = [0, 0];44 params["color"] = "#FFF";4546 // console.log("Dimensions:",xspan, yspan);47 let ports = technology.getPorts(params);48 const drawoffsets = technology.getDrawOffset(params);4950 let ret = [];51 for (let i = 0; i < ports.length; i++) {52 let port = ports[i];53 port.x = Math.floor(port.x + drawoffsets[0]);54 port.y = Math.floor(port.y + drawoffsets[1]);55 ret.push(port.toInterchangeV1());56 }57 console.log("Terminals:", primitive, ret);58 res.send(ret);59};60
61const getDefaults = async (req: Request, res: Response, next: NextFunction) => {
62 console.log(req.query, req.query.mint, req.query["mint"]);
async
function without any await
expressions 5
6paper.setup(new paper.Size([64000, 48000]));
7
8const getDimensions = async (req: Request, res: Response, next: NextFunction) => { 9 let primitive = req.query.mint;10 let key = primitive as string;11 let technology = ComponentAPI.getComponentWithMINT(key);1213 if (technology === null) {14 console.error("Could not find MINT:", key);15 return res.status(400).send({ message: `MINT Not found - ${key}` });16 }1718 let params_text = req.query.params as string;19 let params = JSON.parse(params_text);20 console.log("Params:", params);21 params["position"] = [0, 0];22 params["color"] = "#FFF";23 params["rotation"] = 0;2425 let ret = technology.getDimensions(params);26 console.log("Dimensions:", primitive, ret);27 return res.send({"x-span":ret.xspan, "y-span":ret.yspan});28};29
30const getTerminals = async (req: Request, res: Response, next: NextFunction) => {
31 let primitive = req.query.mint;
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() {}