Skip to content

Commit 4ba829a

Browse files
authored
#5 - add to resolved value of the checkFn to the resolve of the busyw… (#6)
* #5 - add to resolved value of the checkFn to the resolve of the busywait promise * #5 - add to resolved value of the checkFn to the resolve of the busywait promise * #5 - add to resolved value of the checkFn to the resolve of the busywait promise
1 parent dbf9caa commit 4ba829a

File tree

7 files changed

+55
-31
lines changed

7 files changed

+55
-31
lines changed

.jshintrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,8 @@
5050
"validthis": false,
5151
"mocha": true,
5252
"node": true,
53-
"nonstandard": false
53+
"nonstandard": false,
54+
"predef":[
55+
"toString"
56+
]
5457
}

README.md

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ npm install --save busywait
2727

2828
Running:
2929
```js
30-
const busywait = require('busywait').sync;
30+
const busywait = require('../lib/index').sync;
3131

3232
const waitUntil = Date.now() + 2500;
3333

@@ -40,21 +40,22 @@ busywait(syncCheck, {
4040
sleepTime: 500,
4141
maxChecks: 20
4242
})
43-
.then(function (iterations) {
44-
console.log('finished after', iterations, 'iterations');
43+
.then(function (result) {
44+
console.log('finished after', result.iterations, 'iterations', 'with' +
45+
' result', result.result);
4546
});
4647
```
4748
or:
4849
```js
49-
const busywait = require('busywait').async;
50+
const busywait = require('../lib/index').async;
5051

5152
const waitUntil = Date.now() + 2500;
5253

5354
function asyncCheck(iteration) {
5455
return new Promise(function (resolve, reject) {
5556
console.log('running iteration', iteration);
5657
if (Date.now() > waitUntil) {
57-
return resolve();
58+
return resolve(true);
5859
} else {
5960
return reject();
6061
}
@@ -65,8 +66,9 @@ busywait(asyncCheck, {
6566
sleepTime: 500,
6667
maxChecks: 20
6768
})
68-
.then(function (iterations) {
69-
console.log('finished after', iterations, 'iterations');
69+
.then(function (result) {
70+
console.log('finished after', result.iterations, 'iterations', 'with' +
71+
' result', result.result);
7072
});
7173
```
7274
Will result in:
@@ -77,7 +79,7 @@ running iteration 3
7779
running iteration 4
7880
running iteration 5
7981
running iteration 6
80-
finished after 6 iterations
82+
finished after 6 iterations with result true
8183
```
8284

8385
## Methods
@@ -103,8 +105,14 @@ The current iteration number will be passed as first argument to every call of `
103105
#### Return value
104106

105107
Return value is a promise.
106-
The promise will be resolved with the number of iterations passed if the `syncCheckFn` returned true within a legal number of checks.
107-
The promise will be rejected if the `syncCheckFn` rejected `maxChecks` times.
108+
- The promise will be resolved if the `syncCheckFn` returned true within a
109+
legal number of checks.
110+
- The promise will be rejected if the `syncCheckFn` rejected `maxChecks`
111+
times.
112+
113+
Promise resolved value:
114+
- `iterations` - The number of iterations it took to finish
115+
- `result` - Constant `true`
108116

109117
### async(asyncCheckFn, options): Promise
110118

@@ -127,5 +135,10 @@ The current iteration number will be passed as first argument to every call of `
127135
#### Return value
128136

129137
Return value is a promise.
130-
The promise will be resolved with the number of iterations passed if the `asyncCheckFn` was resolved within a legal number of checks.
131-
The promise will be rejected if the `asyncCheckFn` rejected `maxChecks` times.
138+
- The promise will be resolved if the `asyncCheckFn` was resolved within a
139+
legal number of checks.
140+
- The promise will be rejected if the `asyncCheckFn` rejected `maxChecks` times.
141+
142+
Promise resolved value:
143+
- `iterations` - The number of iterations it took to finish
144+
- `result` - The resolved value of `asyncCheckFn`

examples/async.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function asyncCheck(iteration) {
88
return new Promise(function (resolve, reject) {
99
console.log('running iteration', iteration);
1010
if (Date.now() > waitUntil) {
11-
return resolve();
11+
return resolve(true);
1212
} else {
1313
return reject();
1414
}
@@ -19,6 +19,7 @@ busywait(asyncCheck, {
1919
sleepTime: 500,
2020
maxChecks: 20
2121
})
22-
.then(function (iterations) {
23-
console.log('finished after', iterations, 'iterations');
22+
.then(function (result) {
23+
console.log('finished after', result.iterations, 'iterations', 'with' +
24+
' result', result.result);
2425
});

examples/sync.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ busywait(syncCheck, {
1313
sleepTime: 500,
1414
maxChecks: 20
1515
})
16-
.then(function (iterations) {
17-
console.log('finished after', iterations, 'iterations');
16+
.then(function (result) {
17+
console.log('finished after', result.iterations, 'iterations', 'with' +
18+
' result', result.result);
1819
});

lib/index.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"use strict";
22

3-
const _ = require('underscore');
4-
53
module.exports = {
64
sync: sync,
75
async: async
@@ -33,7 +31,7 @@ function _checkArgs(checkFn, options) {
3331
if (isNaN(options.sleepTime) || options.sleepTime < 1) {
3432
return Promise.reject("sleepTime must be a valid integer greater than 0");
3533
}
36-
if (!checkFn || !_.isFunction(checkFn)) {
34+
if (!checkFn || !_isFunction(checkFn)) {
3735
return Promise.reject("checkFn must be a function");
3836
}
3937
return Promise.resolve();
@@ -43,7 +41,7 @@ function _wrapSyncMethod(checkFn) {
4341
return function (iteration) {
4442
return new Promise(function (resolve, reject) {
4543
if (checkFn(iteration)) {
46-
return resolve();
44+
return resolve(true);
4745
} else {
4846
return reject();
4947
}
@@ -57,8 +55,11 @@ function _eventLoop(asyncCheckFn, options) {
5755
const iterationCheck = function () {
5856
iteration++;
5957
asyncCheckFn(iteration)
60-
.then(function checkSuccess() {
61-
return resolve(iteration);
58+
.then(function checkSuccess(result) {
59+
return resolve({
60+
iterations: iteration,
61+
result: result
62+
});
6263
})
6364
.catch(function checkFailed() {
6465
if (iteration === options.maxChecks) {
@@ -70,3 +71,7 @@ function _eventLoop(asyncCheckFn, options) {
7071
setTimeout(iterationCheck, options.waitFirst ? options.sleepTime : 0);
7172
});
7273
}
74+
75+
function _isFunction(obj) {
76+
return toString.call(obj) === '[object Function]';
77+
}

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "busywait",
3-
"version": "1.0.8",
3+
"version": "1.0.9",
44
"description": "Async busy wait",
55
"main": "./lib/index.js",
66
"scripts": {
@@ -25,7 +25,6 @@
2525
},
2626
"homepage": "https://github.com/regevbr/busywait.js#readme",
2727
"dependencies": {
28-
"underscore": "^1.8.3"
2928
},
3029
"devDependencies": {
3130
"coveralls": "^3.0.0",

test/test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('busywait.js', function () {
2121
function asyncCheck() {
2222
return new Promise(function (resolve, reject) {
2323
if (Date.now() > waitUntil) {
24-
return resolve();
24+
return resolve(true);
2525
} else {
2626
return reject();
2727
}
@@ -38,8 +38,9 @@ describe('busywait.js', function () {
3838
sleepTime: 500,
3939
maxChecks: 20
4040
})
41-
.then(function (iterations) {
42-
expect(iterations).to.be(6);
41+
.then(function (result) {
42+
expect(result.iterations).to.be(6);
43+
expect(result.result).to.be(true);
4344
});
4445
});
4546

@@ -49,8 +50,9 @@ describe('busywait.js', function () {
4950
maxChecks: 20,
5051
waitFirst: true
5152
})
52-
.then(function (iterations) {
53-
expect(iterations).to.be(5);
53+
.then(function (result) {
54+
expect(result.iterations).to.be(5);
55+
expect(result.result).to.be(true);
5456
});
5557
});
5658

0 commit comments

Comments
 (0)