I have a business logic library with a method that is designed to run asynchronously, passing false to be used in an if statement. If run asynchronously, it's fine:
const permissions = new PermissionProvider(userId,appId);
if(await permissions.ifAllowed('invalid action', itemID)){
// This code is not reachable.
}
export class PermissionProvider {
...
public async ifAllowed(action: string, itemId?: string): Promise<boolean> {
return await db.query(params)
.then(() => { return false; }
}
However, I'm concerned someone may accidentally run the code synchronously:
if(permissions.ifAllowed('invalid action', itemID)){
// This code executes because asynchronous functions return a Promise,
// and objects evaluate to true.
}
Simply documenting that the API must be run with the async/await seems precarious. This is particularly bad because it may not be obvious that the end user will be allowed to do pretty much anything if the async keyword is omitted.
If I return a primitive boolean instead, the async code then returns undefined because it's coming from the database as a Promise.
Is there a good workaround to either enforcing async calls on the consumer of the API, or preventing the if statement from evaluating true?
I tried throwing an uncaught error, but it still executes the code because the error is thrown after the if statement is already executed.
Aucun commentaire:
Enregistrer un commentaire