See CHANGELOG for a more specific API change history. Although this is a breaking change upgrade, the migration is relatively straightforward since the internals are not changed, just the public facing API.
This document explores
- Motivation for change
- API differences between v9 and v10
- Examples
- How to migrate
The main motivation for creating this breaking change is to make the library more easier to use within larger or more complex applications.
Where multiple API calls can be made in the same scope, and in v10, users need to manually destructure the values err
and res
from the object before renaming them.
- Return type is changed to make it more ergonomic for larger or more complex applications, where multiple calls can be made in the same scope. This change allow users to easily name the destructured return values.
- Original return type
type OLD_ReturnType = Promise<{ res: T, err: undefined } | { res: undefined, err: RequestException }>;
- If there are multiple calls to the library, you need to rename res/err manually since you cant reuse the res/err names within the same scope.
- New return type
type NEW_ReturnType = Promise<readonly [null, T] | readonly [RequestException, null]>;
- The new return type is a tuple, so when destructuring it you already give it a custom name, instead of having to rename it after destructuring from an object.
- Something like React's useState hook, where a component can use multiple useState hooks in a row, and since tuple destructuring allow users to use any names they want, it makes it easier to use.
- Original return type
- Return type is more explicit now
- Instead of relying on default undefined from the destructure process, it is now changed to null to be more explicit about the absence of value.
- Return type is made safer
- The returned tuples are readonly so it can't be modified in userland.
See the sample project for detailed examples.
- Replace all destructuring operations from
const { err, res } = await sf...
toconst [err, res] = await sf...
- Replace all
if (err !== undefined)
checks withif (err !== null)