index.md - v1.2.0
    Preparing search index...

    index.md - v1.2.0

    await-to-done

    Async await wrapper for easy error handling

    Zero dependencies • ES5 compatible • TypeScript ready

    npm version npm downloads gzip License

    Sonar

    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 data
    pnpm add await-to-done
    # or
    npm install await-to-done
    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)
    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)])
    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)
    }
    <script src="https://unpkg.com/await-to-done/dist/index.umd.js"></script>
    <script>
    const [error, data] = await awaitToDone(fetch('/api/data'))
    </script>
    Parameter Type Description
    promise Promise<T> A promise to wrap
    Returns Promise<[E, undefined] | [null, T]> Tuple of error and data
    Parameter Type Description
    promises Promise[] Array of promises
    Returns Promise<[Error, undefined] | [null, P]> Tuple with array of results
    // Result tuple type
    type Result<T, E = Error> = [E, undefined] | [null, T]

    // Async result type
    type AsyncResult<T, E = Error> = Promise<Result<T, E>>
    File Size (min + gzip)
    index.min.mjs ~300 B
    index.umd.js ~400 B
    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

    MIT