Skip to content

virtualstate/promise

Repository files navigation

@virtualstate/promise

Psst... There is a blog post at fabiancook.dev with details on how this project came to be, and the steps taken during implementation to define the included functionality.

Support

Node.js supported Deno supported

Test Coverage

96.23%25 lines covered 96.23%25 statements covered 92.81%25 functions covered 93.06%25 branches covered

all

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")
);
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

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)));
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

Code of Conduct

This project and everyone participating in it is governed by the Code of Conduct listed here. By participating, you are expected to uphold this code. Please report unacceptable behavior to [email protected].

Licence

This repository is licensed under the MIT license.