aasy
Version:
Make Promise without undefined behavior
118 lines (76 loc) • 2.56 kB
Markdown
Async - easy.
Zero-library Promise wrapper without pain.
Make Promise without undefined behavior(almost).
## Reason to use aasy
1. Typescript types. [Promise][1] stantard generic is not accepts Reject generic. aasy does.
2. Less undefined behavior, when using [safe](#safe) function.
3. Inherited from built-in [Promise][1] object
4. [ESM](https://nodejs.org/api/esm.html) and [CJS](https://nodejs.org/api/modules.html) support.
```typescript
type Async<Value, Error = unknown> = intrinsic // from native Promise + API
```
See [contribute guide](./CONTRIBUTING.md)
Call promise and do not throw an error even if get rejected status.
**Arguments**:
`value` - [Promise][1]
**Returns**:
- `Promise<Object>`
- `success` [`boolean`][2]
- `value` - Promise Fulfilled result
- `error` - Promise Reject reason
- `unwrap` `Function` - Function that accepts new error and returns resolved value. See [examples](
```typescript
import Async from 'aasy';
const promiseThatFails = await Async.safe(Async.reject(123));
promiseThatFails.success // false
promiseThatFails.value // undefined
promiseThatFails.error // 123
promiseThatFails.unwrap() // throws "123"
promiseThatFails.unwrap("Undefined Behavior") // throws "Undefined Behavior"
const promiseSuccess = await Async.safe("Hello!");
promiseSuccess.success // true
promiseSuccess.error // undefined
promiseSuccess.value // "Hello!"
promise.unwrap() // "Hello!"
const value = promise.unwrap("Undefined behavior") // error will be ignored
console.log(value) // "Hello!"
```
Transforms `Promise | PromiseLike` value into async instance.
```typescript
import Async from 'aasy';
const promiseFromSomewhere = fetch(...);
const promise = Async.from(promiseFromSomewhere);
// or you can use await to get result from promise
const awaitdResult = await Async.from(promiseFromSomewhere);
```
Inherited from [Promise][1]
Inherited from [Promise][1]
Inherited from [Promise][1]
Inherited from [Promise][1]
Inherited from [Promise][1]
Inherited from [Promise][1]
Inherited from [Promise][1]
- pnpm
- esbuild
- typescript
- husky
- github actions
- jest
- eslint
[]: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise>
[]: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean>