-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
160 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,4 +46,4 @@ | |
"jsdoc-to-markdown": "^4.0.1", | ||
"minami": "^1.2.3" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |