Skip to content

Commit

Permalink
Merge pull request #495 from bekzod/align-things
Browse files Browse the repository at this point in the history
[FEATURE] allow passing promise values to hash/map/hashSettled
  • Loading branch information
stefanpenner committed Jun 11, 2018
2 parents ae7dacd + 3165323 commit 238f66b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
13 changes: 8 additions & 5 deletions lib/rsvp/hash-settled.js
Expand Up @@ -123,9 +123,12 @@ HashSettled.prototype._setResultAt = setSettledResult;
*/

export default function hashSettled(object, label) {
if (object === null || typeof object !== 'object') {
return Promise.reject(new TypeError("hashSettled must be called with an object"), label);
}

return new HashSettled(Promise, object, false, label).promise;
return Promise.resolve(object, label)
.then(function(object) {
if (object === null || typeof object !== 'object') {
throw new TypeError("hashSettled must be called with an object");
}

return new HashSettled(Promise, object, false, label).promise;
});
}
12 changes: 7 additions & 5 deletions lib/rsvp/hash.js
Expand Up @@ -92,9 +92,11 @@ import PromiseHash from './promise-hash';
have been fulfilled, or rejected if any of them become rejected.
*/
export default function hash(object, label) {
if (object === null || typeof object !== 'object') {
return Promise.reject(new TypeError("Promise.hash must be called with an object"), label);
}

return new PromiseHash(Promise, object, label).promise;
return Promise.resolve(object, label)
.then(function(object) {
if (object === null || typeof object !== 'object') {
throw new TypeError("Promise.hash must be called with an object");
}
return new PromiseHash(Promise, object, label).promise;
});
}
12 changes: 7 additions & 5 deletions lib/rsvp/map.js
Expand Up @@ -122,13 +122,15 @@ export class MapEnumerator extends Enumerator {
The promise will be rejected if any of the given `promises` become rejected.
*/
export default function map(promises, mapFn, label) {
if (!Array.isArray(promises)) {
return Promise.reject(new TypeError("map must be called with an array"), label);
}

if (typeof mapFn !== 'function') {
return Promise.reject(new TypeError("map expects a function as a second argument"), label);
}

return new MapEnumerator(Promise, promises, mapFn, label).promise;
return Promise.resolve(promises, label)
.then(function(promises) {
if (!Array.isArray(promises)) {
throw new TypeError("map must be called with an array");
}
return new MapEnumerator(Promise, promises, mapFn, label).promise;
});
}
25 changes: 25 additions & 0 deletions test/extension-test.js
Expand Up @@ -757,6 +757,14 @@ describe("RSVP extensions", function() {
});
});

it('works with promise hash', function(done) {
RSVP.hash(Promise.resolve({}))
.then(function(results) {
assert.deepEqual(results, {}, 'expected fulfillment');
done();
}).catch(done);
});

it('works with null', function(done) {
RSVP.hash({foo: null}).then(function(results) {
assert.deepEqual(results.foo, null);
Expand Down Expand Up @@ -831,6 +839,14 @@ describe("RSVP extensions", function() {
assert(RSVP.hashSettled);
});

it('works with promise hash', function(done) {
RSVP.hashSettled(Promise.resolve({}))
.then(function(results) {
assert.deepEqual(results, {}, 'expected fulfillment');
done();
}).catch(done);
});

it('fulfilled only after all of the promise values are fulfilled', function(done) {
var firstResolved, secondResolved, firstResolver, secondResolver;

Expand Down Expand Up @@ -2836,6 +2852,15 @@ describe("RSVP extensions", function() {
});
});

it("works promise array", function(done){
var values = RSVP.resolve([ 1, RSVP.resolve(2) ]);

RSVP.map(values, mapFn).then(function(results){
assert.deepEqual([2, 3], results);
done();
}).catch(done);
});

it("becomes rejected with the first promise that becomes rejected", function(done){

var promises = [
Expand Down

0 comments on commit 238f66b

Please sign in to comment.