-
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
10 changed files
with
240 additions
and
0 deletions.
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
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
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
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,38 @@ | ||
import Promises from '@promises/core'; | ||
import { IOptionalPromise } from '@promises/interfaces'; | ||
import doWhileSeries from './'; | ||
|
||
Promises._setOnConstructor('doWhileSeries', doWhileSeries); | ||
|
||
export default doWhileSeries; | ||
|
||
declare module '@promises/core' { | ||
namespace Promises { | ||
/** | ||
* @example | ||
* | ||
* ```typescript | ||
* let index: number = 0; | ||
* Promises.doWhileSeries(() => { | ||
* console.log(`test ${index}`); | ||
* return index++ < 3; | ||
* }, () => Promises.timeout((resolve) => { | ||
* console.log(`iteratee ${index}`); | ||
* resolve(); | ||
* }, 4 - index)).then(() => { | ||
* console.log('completed'); | ||
* }); | ||
* // => 'iteratee 0' | ||
* // => 'test 0' | ||
* // => 'iteratee 1' | ||
* // => 'test 1' | ||
* // => 'iteratee 2' | ||
* // => 'test 2' | ||
* // => 'iteratee 3' | ||
* // => 'test 3' | ||
* // => 'completed' | ||
* ``` | ||
*/ | ||
export function doWhileSeries(test: () => IOptionalPromise<boolean>, iteratee?: () => IOptionalPromise<any>): Promises<void>; | ||
} | ||
} |
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,50 @@ | ||
/** | ||
* @module @promises/do-while-series | ||
* @copyright © 2017 Yisrael Eliav <yisraelx@gmail.com> (https://github.com/yisraelx) | ||
* @license MIT | ||
*/ | ||
|
||
import { IOptionalPromise } from '@promises/interfaces'; | ||
import exec from '@promises/exec'; | ||
|
||
|
||
/** | ||
* @example | ||
* | ||
* ```typescript | ||
* let index: number = 0; | ||
* doWhileSeries(() => { | ||
* console.log(`test ${index}`); | ||
* return index++ < 3; | ||
* }, () => timeout((resolve) => { | ||
* console.log(`iteratee ${index}`); | ||
* resolve(); | ||
* }, 4 - index)).then(() => { | ||
* console.log('completed'); | ||
* }); | ||
* // => 'iteratee 0' | ||
* // => 'test 0' | ||
* // => 'iteratee 1' | ||
* // => 'test 1' | ||
* // => 'iteratee 2' | ||
* // => 'test 2' | ||
* // => 'iteratee 3' | ||
* // => 'test 3' | ||
* // => 'completed' | ||
* ``` | ||
*/ | ||
function doWhileSeries(test: () => IOptionalPromise<boolean>, iteratee: () => IOptionalPromise<any> = () => { }): Promise<void> { | ||
return new Promise((resolve, reject) => { | ||
let each = () => { | ||
exec(iteratee).then(() => { | ||
return exec(test).then((isPass: boolean) => { | ||
isPass ? each() : resolve(); | ||
}); | ||
}).catch(reject); | ||
}; | ||
|
||
each(); | ||
}); | ||
} | ||
|
||
export default doWhileSeries; |
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 @@ | ||
{ | ||
"name": "@promises/do-while-series", | ||
"version": "NEXT-PLACEHOLDER", | ||
"description": "Do While Series is package from Promises library", | ||
"main": "es5.js", | ||
"browser": "umd.min.js", | ||
"module": "index.js", | ||
"es2015": "index.js", | ||
"typings": "index.d.ts", | ||
"bundle": "bundle.min.js", | ||
"author": { | ||
"name": "Yisrael Eliev", | ||
"url": "https://github.com/yisraelx", | ||
"email": "yisraelx@gmail.com" | ||
}, | ||
"license": "MIT", | ||
"keywords": [ | ||
"promise", | ||
"promises", | ||
"utility", | ||
"modules", | ||
"async", | ||
"await", | ||
"deferred" | ||
], | ||
"homepage": "https://github.com/yisraelx/promises#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/yisraelx/promises.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/yisraelx/promises/issues" | ||
}, | ||
"optionalDependencies": { | ||
"@promises/core": "^0.2.0" | ||
}, | ||
"dependencies": { | ||
"@promises/exec": "^0.2.0", | ||
"@promises/interfaces": "^0.2.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,105 @@ | ||
import doWhileSeries from '@promises/do-while-series'; | ||
import timeout from '@promises/timeout'; | ||
|
||
describe('doWhileSeries', () => { | ||
|
||
it('should be iteratee once if test not pass', () => { | ||
let test = 0; | ||
let iteratee = 0; | ||
return doWhileSeries(() => { | ||
test++; | ||
return false; | ||
}, () => { | ||
iteratee++; | ||
}).then(() => { | ||
expect(test).toBe(1); | ||
expect(iteratee).toBe(1); | ||
}); | ||
}); | ||
|
||
it('should be iteratee without iteratee function', () => { | ||
let index = 0; | ||
return doWhileSeries(() => { | ||
return index++ < 5; | ||
}).then(() => { | ||
expect(index).toBe(6); | ||
}); | ||
}); | ||
|
||
it('should be reject on test error', () => { | ||
return doWhileSeries(() => { | ||
throw 'test'; | ||
}).then(() => { | ||
throw 'resolve'; | ||
}).catch((error) => { | ||
expect(error).toBe('test'); | ||
}); | ||
}); | ||
|
||
it('should be reject on iteratee error', () => { | ||
return doWhileSeries(() => { | ||
throw 'test'; | ||
}, () => { | ||
throw 'iteratee'; | ||
}).then(() => { | ||
throw 'resolve'; | ||
}).catch((error) => { | ||
expect(error).toBe('iteratee'); | ||
}); | ||
}); | ||
|
||
it('should be iteratee series 6 times', () => { | ||
let index = 0; | ||
let length = 5; | ||
let test = []; | ||
let iteratee = []; | ||
return doWhileSeries(() => { | ||
test.push(index); | ||
return index++ < length; | ||
}, () => { | ||
iteratee.push(index); | ||
}).then(() => { | ||
expect(index).toBe(6); | ||
expect(test).toEqual([0, 1, 2, 3, 4, 5]); | ||
expect(iteratee).toEqual([0, 1, 2, 3, 4, 5]); | ||
}); | ||
}); | ||
|
||
it('should be iteratee series 6 times with promise iteratee', () => { | ||
let index = 0; | ||
let test = []; | ||
let iteratee = []; | ||
return doWhileSeries(() => { | ||
test.push(index); | ||
return index++ < 5; | ||
}, () => { | ||
let thisIndex = index; | ||
return timeout((resolve) => { | ||
iteratee.push(thisIndex); | ||
resolve(); | ||
}, 6 - index); | ||
}).then(() => { | ||
expect(index).toBe(6); | ||
expect(test).toEqual([0, 1, 2, 3, 4, 5]); | ||
expect(iteratee).toEqual([0, 1, 2, 3, 4, 5]); | ||
}); | ||
}); | ||
|
||
it('should be iteratee series 6 times with promise test', () => { | ||
let index = 0; | ||
let test = []; | ||
let iteratee = []; | ||
return doWhileSeries(() => { | ||
return timeout((resolve) => { | ||
test.push(index); | ||
resolve(index++ < 5); | ||
}); | ||
}, () => { | ||
iteratee.push(index); | ||
}).then(() => { | ||
expect(index).toBe(6); | ||
expect(test).toEqual([0, 1, 2, 3, 4, 5]); | ||
expect(iteratee).toEqual([0, 1, 2, 3, 4, 5]); | ||
}); | ||
}); | ||
}); |