await-to-done
v1.2.0
Published
Async await wrapper for easy error handling - zero dependencies
Maintainers
Readme
await-to-done
Async await wrapper for easy error handling
Zero dependencies • ES5 compatible • TypeScript ready
Why?
Stop writing try-catch blocks everywhere. await-to-done wraps your promises and returns a tuple [error, data] for elegant error handling.
// Before 😫
try {
const data = await fetchData()
// handle data
} catch (error) {
// handle error
}
// After 😍
import to from 'await-to-done'
const [error, data] = await to(fetchData())
if (error) {
// handle error
}
// handle dataInstall
pnpm add await-to-done
# or
npm install await-to-doneUsage
Single Promise
import to from 'await-to-done'
const [error, user] = await to(fetchUser(id))
if (error) {
console.error('Failed to fetch user:', error)
return
}
console.log('User:', user)Multiple Promises
import to from 'await-to-done'
// Pass multiple promises as arguments
const [error, [user, posts]] = await to(fetchUser(id), fetchPosts(id))
// Or pass an array
const [error, results] = await to([fetchUser(id), fetchPosts(id)])Custom Error Type
import to from 'await-to-done'
interface ApiError {
code: number
message: string
}
const [error, data] = await to<string, ApiError>(fetchData())
if (error) {
console.log(error.code, error.message)
}Browser / CDN
<script src="https://unpkg.com/await-to-done/dist/index.umd.js"></script>
<script>
const [error, data] = await awaitToDone(fetch('/api/data'))
</script>API
to<T, E>(promise)
| Parameter | Type | Description |
| ----------- | -------------------------------------- | ----------------------- |
| promise | Promise<T> | A promise to wrap |
| Returns | Promise<[E, undefined] \| [null, T]> | Tuple of error and data |
to<P>(promises)
| Parameter | Type | Description |
| ----------- | ------------------------------------------ | --------------------------- |
| promises | Promise[] | Array of promises |
| Returns | Promise<[Error, undefined] \| [null, P]> | Tuple with array of results |
Types
// Result tuple type
type Result<T, E = Error> = [E, undefined] | [null, T]
// Async result type
type AsyncResult<T, E = Error> = Promise<Result<T, E>>Bundle Size
| File | Size (min + gzip) |
| --------------- | ----------------- |
| index.min.mjs | ~300 B |
| index.umd.js | ~400 B |
Comparison
| Feature | await-to-done | await-to-js | | ----------------- | ------------- | ----------- | | Dependencies | 0 | 0 | | TypeScript types | ✅ Built-in | ❌ Separate | | Multiple promises | ✅ | ❌ | | Array support | ✅ | ❌ | | ES5 compatible | ✅ | ✅ | | Size | ~300 B | ~200 B |
