It's the same for async and non-async functions really:
true
/false
can be a legitimate answer to the posed question
- an exception/promise rejection is a sign of a serious failure
For example, if the purpose of your function is to determine whether a string is numeric, the return values true
and false
have an obvious meaning. However, if you're passing in an object, which isn't a string to begin with, an exception is appropriate since the answer to the question "is this string numeric" for an object is "mu" ???♂?.
For an async function the same holds true: if it takes a network request to get a true
/false
answer for some question, then resolving the promise with true
/false
is entirely cromulent; e.g. if you're checking with the server whether an entered user name is still available, "yes" and "no" are valid answers. If the network request itself fails however, that would be an exceptional failure, a "mu", which you need to signal by rejecting the entire promise.
A promise rejection is just an asynchronous exception; with the await
syntax it is even handled the same as a synchronous exception. An exception is used for exceptional events, when a normal answer cannot be provided due to circumstances beyond the normal happy path.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…