Skip to content

Commit

Permalink
Async do while and while do
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevnz committed Apr 24, 2019
1 parent 0db4828 commit e61dcb3
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@
"jsdoc-to-markdown": "^4.0.1",
"minami": "^1.2.3"
}
}
}
31 changes: 31 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,37 @@ const { timeout } = require('@kev_nz/async-tools')

})()
```

### Do While

Execute an async function while a condition is true

```javascript
const { doWhile } = require('@kev_nz/async-tools')

(async () => {
const results = await doWhile(async () => {
await asyncFunction()
}, () => something === true)
})()
```

### While Do

While a condition is true execute an async function

```javascript
const { whileDo } = require('@kev_nz/async-tools')

(async () => {
const results = await whileDo(
() => something === true),
async () => {
await asyncFunction()
})
})()
```

### Test Coverage

* [Coveralls](https://coveralls.io/github/Kevnz/async-tools?branch=master)
Expand Down
41 changes: 41 additions & 0 deletions src/__tests__/do-while.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const delay = require('../delay')
const doWhile = require('../do-while')

describe('The DoWhile function', () => {
it('should while a condition function is true execute an async function', async () => {
let inc = 0
const condition = () => inc < 100
const activity = async () => {
inc++
await delay(10)
}
await doWhile(activity, condition)
expect(inc).toBe(100)
})

it('should while a condition function is true execute an async function', async () => {
let inc = 0
let functionCount = 0
const condition = () => {
inc++
return inc < 100
}
const activity = async () => {
functionCount++
await delay(10)
}
await doWhile(activity, condition)
expect(inc).toBe(100)
expect(functionCount).toBe(100)
})
it('should execute the function at least one time', async () => {
let inc = 0
const condition = () => false
const activity = async () => {
inc++
await delay(10)
}
await doWhile(activity, condition)
expect(inc).toBe(1)
})
})
43 changes: 43 additions & 0 deletions src/__tests__/while-do.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const delay = require('../delay')
const whileDo = require('../while-do')

describe('The WhileDo function', () => {
it('should execute a function while a condition function is true', async () => {
let inc = 0
const condition = () => {
inc++
return inc < 100
}
const activity = async () => {
await delay(10)
}
await whileDo(condition, activity)
expect(inc).toBe(100)
})
it('should execute a function while a condition function is true and not execute it when it becomes false', async () => {
let inc = 0
let functionCount = 0
const condition = () => {
inc++
return inc < 100
}
const activity = async () => {
functionCount++
await delay(10)
}
await whileDo(condition, activity)
expect(inc).toBe(100)
expect(functionCount).toBe(99)
})

it('should not execute the function even one time if condition is not true', async () => {
let inc = 0
const condition = () => false
const activity = async () => {
inc++
await delay(10)
}
await whileDo(condition, activity)
expect(inc).toBe(0)
})
})
23 changes: 23 additions & 0 deletions src/do-while.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Execute an async function while a condition is true
*
* @async
* @function doWhile
* @param {function} func - An async function to execute
* @param {function} condition - The condition function to check
*
*
* @example
* const results = await doWhile(async () => {
* await asyncFunction()
* }, () => something === true)
*/
const doWhile = async (func, condition) => {
await func()

if (condition()) {
return doWhile(func, condition)
}
}

module.exports = doWhile
21 changes: 21 additions & 0 deletions src/while-do.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* While a condition is true execute an async function
*
* @async
* @function whileDo
* @param {function} condition - The condition function to check
* @param {function} func - An async function to execute
*
* @example
* const results = await doWhile(async () => {
* await asyncFunction()
* }, () => something === true)
*/
const whileDo = async (condition, func) => {
if (condition()) {
await func()
return whileDo(condition, func)
}
}

module.exports = whileDo

0 comments on commit e61dcb3

Please sign in to comment.