https://github.com/virtualstate/promise
Async tools
https://github.com/virtualstate/promise
async-await async-iterables deno iterator-helpers javascript promise typescript
Last synced: 17 days ago
JSON representation
Async tools
- Host: GitHub
- URL: https://github.com/virtualstate/promise
- Owner: virtualstate
- License: mit
- Created: 2022-02-23T09:59:07.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-06-14T09:44:11.000Z (11 months ago)
- Last Synced: 2025-04-10T23:14:30.367Z (17 days ago)
- Topics: async-await, async-iterables, deno, iterator-helpers, javascript, promise, typescript
- Language: TypeScript
- Homepage:
- Size: 504 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: CODE-OF-CONDUCT.md
Awesome Lists containing this project
README
# `@virtualstate/promise`
> Psst... There is a blog post at [fabiancook.dev](https://fabiancook.dev/2022/02/26/an-async-thing) with details on how
> this project came to be, and the steps taken during implementation to define the included functionality.[//]: # (badges)
### Support
 
### Test Coverage
   
[//]: # (badges)
## `all`
```typescript
import { all } from "@virtualstate/promise";// logs []
console.log(await all());
// logs [1, 2]
console.log(await all(Promise.resolve(1), Promise.resolve(2)));
// logs rejected
console.log(
await all(Promise.resolve(1), Promise.reject(2))
.catch(() => "rejected")
);
``````typescript
const wait = (timeout = 1, arg = undefined) => new Promise(resolve => setTimeout(resolve, timeout, arg));
for await (const state of all(
wait(10, "first index, second resolve"),
wait(1, "second index, first resolve")
)) {
/*
logs
{ state: [undefined, "second index, first resolve"] }
{ state: ["first index, second resolve", "second index, first resolve"] }
*/
console.log({ state });
}
```## `allSettled`
```typescript
import { allSettled } from "@virtualstate/promise";// logs []
console.log(await allSettled());
// logs [
// { value: 1, status: 'fulfilled' },
// { value: 2, status: 'fulfilled' }
// ]
console.log(await allSettled(Promise.resolve(1), Promise.resolve(2)));
// logs [
// { value: 1, status: 'fulfilled' },
// { reason: 2, status: 'rejected' }
// ]
console.log(await allSettled(Promise.resolve(1), Promise.reject(2)));
``````typescript
const wait = (timeout = 1, arg = undefined) => new Promise(resolve => setTimeout(resolve, timeout, arg));
for await (const state of allSettled(
wait(10, "A"),
wait(1, "B"),
wait(15).then(() => Promise.reject("C"))
)) {
/*
logs
{
state: [ undefined, { value: 'B', status: 'fulfilled' }, undefined ]
}
{
state: [
{ value: 'A', status: 'fulfilled' },
{ value: 'B', status: 'fulfilled' },
undefined
]
}
{
state: [
{ value: 'A', status: 'fulfilled' },
{ value: 'B', status: 'fulfilled' },
{ reason: 'C', status: 'rejected' }
]
}
*/
console.log({ state });
}
```## Contributing
Please see [Contributing](./CONTRIBUTING.md)
## Code of Conduct
This project and everyone participating in it is governed by the [Code of Conduct listed here](./CODE-OF-CONDUCT.md). By participating, you are expected to uphold this code. Please report unacceptable behavior to [[email protected]](mailto:[email protected]).
## Licence
This repository is licensed under the [MIT](https://choosealicense.com/licenses/mit/) license.