Skip to content

async function should have await expression JS-0116 #39

@philipjonsen

Description

@philipjonsen

DESCRIPTION

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:

The return value is always a Promise.
You can use the 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
}

// 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.

BAD PRACTICE
async function fetchData(): string {
// readFileSync is a synchronous function that blocks
// the main thread, and thus does not need to be awaited
return fs.readFileSync("data.txt", "utf-8");
}

performAction(async () => { console.log("no awaits in here") });
RECOMMENDED
async function fetchDataAsync(): Promise {
return await fs.readFile("data.txt", "utf-8")
}

performAction(async () => { await writeToFile(data) });

// Allow empty functions.
async function no_op() {}

Look here to fix it:

Found async function without any await expressions
src/deterministic-wallet.ts

return node.neuter().extendedKey;

}

async getAddressesWithMultipleDPaths(
input: {
path: DerivationPath;
limit: number;
offset?: number;
}[]
): Promise<DeterministicAddress[]> {
const promises = input.map(({ path, limit, offset }) => () =>
this.getAddresses({ path, limit, offset })
);
return sequence(promises).then((results) => results.flat());
}

async getAddresses({
path,
Found async function without any await expressions
src/implementations/deterministic/gridplus.test.ts

});

describe('getCredentials', () => {
it('returns credentials', async () => {
const wallet = new GridPlusWallet(config);
expect(wallet.getCredentials()).toStrictEqual({
deviceID: config.deviceID,
password: config.password
});
});
});
});
Found async function without any await expressions
src/implementations/deterministic/gridplus.ts

return this.address;

}

async getPrivateKey(): Promise {
throw new Error('Method not implemented.');
}
}

export class GridPlusWallet extends HardwareWallet {
Found async function without any await expressions
src/implementations/deterministic/gridplus.ts

return wallet.getAddress();

}

async getExtendedKey(_path: string): Promise<{ publicKey: string; chainCode: string }> {
throw new Error('Method not implemented.');
}

async getHardenedAddress(path: DerivationPath, index: number): Promise {
return this.getAddress(path, index);
Found async function without any await expressions
src/implementations/deterministic/gridplus.ts

throw new Error('Method not implemented.');

}

async getHardenedAddress(path: DerivationPath, index: number): Promise {
return this.getAddress(path, index);
}

// Manually scan for addresses since extended key information is not available currently
async getAddresses({Found async function without any await expressions
src/deterministic-wallet.ts

return node.neuter().extendedKey;

}

async getAddressesWithMultipleDPaths(
input: {
path: DerivationPath;
limit: number;
offset?: number;
}[]
): Promise<DeterministicAddress[]> {
const promises = input.map(({ path, limit, offset }) => () =>
this.getAddresses({ path, limit, offset })
);
return sequence(promises).then((results) => results.flat());
}

async getAddresses({
path,
Found async function without any await expressions
src/implementations/deterministic/gridplus.test.ts

});

describe('getCredentials', () => {
it('returns credentials', async () => {
const wallet = new GridPlusWallet(config);
expect(wallet.getCredentials()).toStrictEqual({
deviceID: config.deviceID,
password: config.password
});
});
});
});
Found async function without any await expressions
src/implementations/deterministic/gridplus.ts

return this.address;

}

async getPrivateKey(): Promise {
throw new Error('Method not implemented.');
}
}

export class GridPlusWallet extends HardwareWallet {
Found async function without any await expressions
src/implementations/deterministic/gridplus.ts

return wallet.getAddress();

}

async getExtendedKey(_path: string): Promise<{ publicKey: string; chainCode: string }> {
throw new Error('Method not implemented.');
}

async getHardenedAddress(path: DerivationPath, index: number): Promise {
return this.getAddress(path, index);
Found async function without any await expressions
src/implementations/deterministic/gridplus.ts

throw new Error('Method not implemented.');

}

async getHardenedAddress(path: DerivationPath, index: number): Promise {
return this.getAddress(path, index);
}

// Manually scan for addresses since extended key information is not available currently
async getAddresses({

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions