Skip to content

Commit cf22407

Browse files
committed
fix while logic
1 parent aaa8907 commit cf22407

4 files changed

Lines changed: 14 additions & 6 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ To use while2, first import the module. It is recommended to call it `w2`.
1414
const w2 = require('while2')
1515
```
1616

17-
For every while2 statement, you will create a new instance of `w2`, as it is a class, and pass a condition. For everything that you want to run while the condition is true, put `.do(...)` and then pass in a function in the parentheses. Always type `.end()`at the end to run the while loop or it won't work
17+
For every while2 statement, you will create a new instance of `w2`, as it is a class, and pass a condition. The condition must be a function that returns something. For everything that you want to run while the condition is true, put `.do(...)` and then pass in a function in the parentheses. Always type `.end()`at the end to run the while loop or it won't work
1818

1919
```javascript
2020
const w2 = require('while2')
2121

22-
new w2(true) // equivalent to while (true)
22+
new w2(() => true) // equivalent to while (true)
2323
.do(() => {
2424
// anything here will run again and again forever
2525
})
@@ -33,7 +33,7 @@ While2 includes an i parameter that will automatically increment every time the
3333
```javascript
3434
const w2 = require('while2')
3535

36-
new w2(true)
36+
new w2(() => true)
3737
.do(i => {
3838
console.log(i) /* will print 1 2 3 4 5 6 7... */
3939
})
@@ -47,7 +47,7 @@ While2 includes a `breakLoop` parameter function so that you can break your whil
4747
```javascript
4848
const w2 = require('while2')
4949

50-
new w2(true)
50+
new w2(() => true)
5151
.do((i, breakLoop) => {
5252
if (i > 3) breakLoop() // prints 0, 1, 2, 3 then stops
5353
console.log(i)

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class w2 {
1313
}
1414
end() {
1515
try {
16-
while (this.condition) {
16+
while (this.condition()) {
1717
this.doHandler(this.i, () => {
1818
throw breakError
1919
}) // pass in the function to break the loop

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "while2",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"description": "while in fp",
55
"keywords": [
66
"while",

t.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var w2 = require('./index')
2+
var i =0;
3+
new w2(() => i < 10)
4+
.do(() => {
5+
console.log(i)
6+
i++
7+
})
8+
.end()

0 commit comments

Comments
 (0)