Settle

settle awaits a promise and hands you its error instead of throwing it — so you can handle a failure with an early return rather than wrapping the call in a try/catch block.


Use await without try/catch

await makes promises look nice and synchronous, but it forces you to use try/catch blocks to catch errors. That looks ugly.

This makes it pretty with good ol’ callback-style values.

Before
let user
try {
user = await getUser(id)
} catch (error) {
// Do something with error
}
After
const [user, error] = await settle(getUser(id))
if (error) {
// Do something with error
}

Await multiple promises together like Promise.all

Pass an array of promises and settle awaits them together.

Every value comes back in the order you passed them, just like Promise.all, with the error at the end.

Before
let user
let posts
try {
[user, posts] = await Promise.all([getUser(id), getPosts(id)])
} catch (error) {
// Do something with error
}
After
const [user, posts, error] = await settle([getUser(id), getPosts(id)])
if (error) {
// Do something with error
}