Skip to content

promise 1.2.0

Install from the command line:
Learn more about npm packages
$ npm install @virtualstate/promise@1.2.0
Install via package.json:
"@virtualstate/promise": "1.2.0"

About this version

@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

95.97%25 lines covered 95.97%25 statements covered 92.64%25 functions covered 92.76%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.

Details


Assets

  • promise-1.2.0.tgz

Download activity

  • Total downloads 0
  • Last 30 days 0
  • Last week 0
  • Today 0