Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE REQUEST] Provide convenient Promise.propsSettled method #1662

Open
matthewadams opened this issue Sep 25, 2020 · 0 comments
Open

[FEATURE REQUEST] Provide convenient Promise.propsSettled method #1662

matthewadams opened this issue Sep 25, 2020 · 0 comments

Comments

@matthewadams
Copy link

  1. What version of bluebird is the issue happening on?
    3.7.2

  2. What platform and version? (For example Node.js 0.12 or Google Chrome 32)
    Node.js 12.18.4

  3. Did this issue happen with earlier version of bluebird?
    N/A

Description

Provide a convenient method that is similar to Promise.allSettled, but that is called Promise.propsSettled and takes an object and returns an object with keys corresponding to the given values. Here's a working proof of concept:

/* global describe, it */
'use strict'

const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect

const Promise = require('bluebird')

Promise.propsSettled = async function (o) {
  const keys = Object.keys(o).sort()
  const results = await Promise.allSettled(keys.map(key => o[key]))

  return results.reduce((accum, result, index) => {
    accum[keys[index]] = results[index]
    return accum
  }, {})
}

describe('unit test of Promise.props', function () {
  it('should work', async function () {
    const one = 1
    const two = new Error('boom')
    const three = 3

    const result = await Promise.propsSettled({
      one: Promise.resolve(one),
      two: Promise.reject(two),
      three: Promise.resolve(three)
    })

    expect({
      one: result.one.value(),
      two: result.two.error(),
      three: result.three.value()
    }).to.deep.equal({ one, two, three })
  })
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant