Skip to content

Commit

Permalink
Merge pull request #10 from B-Vladi/fix/get-result
Browse files Browse the repository at this point in the history
Fix Response#getResult
  • Loading branch information
Vlad Kurkin committed Feb 4, 2016
2 parents 5a07afc + f59a1d6 commit 70ef731
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 26 deletions.
40 changes: 21 additions & 19 deletions Response.js
Original file line number Diff line number Diff line change
Expand Up @@ -940,34 +940,36 @@ Response.prototype.map = function (listener, context) {
* @throws {Error}
*/
Response.prototype.getResult = function (key) {
if (this.isResolved()) {
if (typeof key === 'string' || typeof key === 'number') {
return getResponseResults(this.getStateData(key));
}
if (this.isRejected()) {
return;
}

if (this.keys.length === 0) {
return getResponseResults(this.stateData[0]);
}
if (typeof key === 'string' || typeof key === 'number') {
return getResponseResults(this.getStateData(key));
}

var _key;
var keys = isArray(key) ? key : this.keys;
var length = this.stateData.length;
var index = 0;
var result = {};
if (this.keys.length === 0) {
return getResponseResults(this.stateData[0]);
}

while (index < length) {
_key = keys[index];
var _key;
var keys = isArray(key) ? key : this.keys;
var length = this.stateData.length;
var index = 0;
var result = {};

if (_key != null) {
result[_key] = getResponseResults(this.stateData[index]);
}
while (index < length) {
_key = keys[index];

index++;
if (_key != null) {
result[_key] = getResponseResults(this.stateData[index]);
}

return result;
index++;
}

return result;

};

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Response",
"author": "Vladislav Kurkin <[email protected]> (https://github.com/B-Vladi)",
"version": "0.1.2",
"version": "0.1.3",
"main": "Response.js",
"license": "MIT",
"repository": {
Expand Down
22 changes: 18 additions & 4 deletions spec/Queue.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,15 @@ describe('Queue:', function () {
.push(function key1 () {
this.push(listener, 'key3');
})
.push({
name: 'key2'
.push(function key2 () {
this
.push(listener, 'key4')
.push(listener, 'key5');
})
.start();

expect(listener.calls.count()).toBe(1);
expect(queue.keys).toEqual(['key0', 'key1', 'key2', 'key3']);
expect(listener.calls.count()).toBe(3);
expect(queue.keys).toEqual(['key0', 'key1', 'key2', 'key3', 'key4', 'key5']);
});
});

Expand Down Expand Up @@ -622,4 +624,16 @@ describe('Queue:', function () {
}], true);
});
});

it('getResult should be returns result if state is pending', function () {
new Queue()
.push(1, 'one')
.push(function () {
expect(this.getResult('one')).toBe(1);
expect(this.getResult()).toEqual({
one: 1
});
})
.start();
})
});
4 changes: 2 additions & 2 deletions spec/Response.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,8 +595,8 @@ describe('Response:', function () {
});

describe('getResult', function () {
it('should return undefined if not resolved', function () {
expect(resp.getResult()).toBeUndefined();
it('should return undefined if rejected', function () {
expect(resp.setState(1, [2]).getResult()).toBe(2);
expect(resp.reject('error').getResult()).toBeUndefined();
});

Expand Down

0 comments on commit 70ef731

Please sign in to comment.