outputs-not-outcomes
v0.4.0
Published
Using Promise as Result
Readme
outputs-not-outcomes
Using Javascript Promises as Results
Install
npm i -S outputs-not-outcomesUsage
This package is to be used exactly like Promise, the only difference being the typing of errors.
Constructing a type-safe result
const result = new Result<MyCustomError, MyResult>((success, failure) => {
success('my success here')
// or
failure(new MyCustomError())
})
const resolved = Result.resolve(1) // Result<never, number>
const rejected = Result.reject(1) // Result<number, never>Building pipelines
See register.ts, roughly:
function register(email: string, password: string) {
return validatePassword(password) // sync check
.then(() => validateEmail(email))
.then(() => assertNoAccountExistsWithEmail(email)) // async check
.then(() => createAccount(email, password)) // async task
}FAQ
Why do I sometimes have RuntimeError and sometimes not ?
It depends on how you constructed the result. For instance, Result.resolve(1) is entirely safe, there's no chance a type error or whatever would slip in the 1 statement.
However when resolving a Result from a Promise (or PromiseLike), we have no guarantee that the underlying Promise or PromiseLike does not embed any runtime error. Since there's a risk, there's a possibility. Therefore is it is typed.
What can introduce a runtime error:
new Result((resolve, reject) => { … })-> the function body may contain a runtime error.Result.resolve(promiseLike)->promiseLikemay contain a runtime error.Result.reject(promiseLike)->promiseLikemay contain a runtime error.myResult.then(a, b)->aorbfunction bodies may contain a runtime error.myResult.catch(cb)->cbfunction body may contain a runtime error.
