petkaantonov/bluebird

[FEATURE REQUEST] Provide convenient Promise.propsSettled method

matthewadams opened this issue · 0 comments

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