I can use assert from nodejs instead of if condition in nodejs?

Viewed 13

I can use Assert function from nodejs instead of if condition in nodejs: import assert from 'node:assert'; it's meant to testing only or I can use in my functions which are not tests?

For example I don't use if, only assert:

async function getUser() {
 const user = getDb().selectUsers().first();

  assert(user, new Error('USER_NOT_FOUND'));
  // in if I'll do: if (!user) throw new Error('USER_NOT_FOUND');
   
  console.log({ foundUser: user });
  ...

}

This is something okay to do? or it's bad pattern?

1 Answers

Assert is meant to be used for testing and not performing logic.

Related