async
keyword JS-0376 346 testIgnore(t, pattern, ignored, unignored);
347});
348
349test('Ignore Manager [foo/bar[1-4]]', async (t) => { 350 const pattern = ['foo/bar[1-4].jpg'];
351
352 const ignored = [
329 testIgnore(t, pattern, ignored, unignored);
330});
331
332test('Ignore Manager [debug?.log]', async (t) => { 333 // A question mark matches exactly one character.
334 const pattern = ['debug?.log'];
335
308 }
309});
310
311test('Ignore Manager [debug[01].log]', async (t) => { 312 // Square brackets match a single character form the specified set.
313 const pattern = ['debug[01].log'];
314
288 }
289});
290
291test('Ignore Manager [foo/*/baz], [foo/*/baz/], [foo/*/baz/**]', async (t) => { 292 const patterns = [['foo/*/baz'], ['foo/*/baz/'], ['foo/*/baz/**']];
293 for (const pattern of patterns) {
294 const ignored = [
266 testIgnore(t, pattern, ignored, unignored);
267});
268
269test('Root Test 2 [/foo/bar] [/foo/bar/] [/foo/bar/**]', async (t) => { 270 for (const pattern of [['/foo/bar'], ['/foo/bar/'], ['/foo/bar/**']]) {
271 const ignored = [
272 'foo/bar',
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 promise1() {
return 1
}
async function fetchJSON(url: string) {
return axios.get(url)
}
function promise1() {
return Promise.resolve(1);
}
async function fetchJSON(url: string) {
const data = await axios.get(url)
return data.payload;
}