Smith
Smith

Reputation: 1428

How to throw an error if promise is not awaited?

I was dealing with a function that returns a promise, which was a simple Boolean. The problem is that it was something important, and I forget to use "await", so it always has a true value. I only discovered accidentally after several tests. I wonder if there is a way to raise an error if await was not used to avoid any future issue.

Here goes some code to ilustrate the problem:

async function isAdm(message) {
    let chat = await message.getChat();

    let admList = await getAdmsList(chat);
    let author = message.author;

    return admList.includes(author);
}

console.log(isAdm(message));
console.log(Boolean(isAdm(message)));
>>> Promise { <pending> }
>>> true

Upvotes: 0

Views: 72

Answers (1)

novarx
novarx

Reputation: 525

Well, no I don't think there would be a reasonable "Exception-like" solution to this.

The best approach to ensure the correct behaviour of such issues - are probably Unit-Tests.

Upvotes: 1

Related Questions