Promises is utilities modules for promises, written in typescript.
- The code is divided into many small modules and each module is a package in itself (Packages List).
- Each package in the scoop has only one function or class and it in the default export of the package.
- Group package brings together several modules from the scope, and in the export has access to all modules.
Marks:
- "-": @{scope}/-{name} - Group package
- "_": @{scope}/_{name} - Internal package
Installation of a group of all packages in scoop @promises
$ npm install --save @promises/-all
Installation of a single package from scoop @promises
$ npm install --save @promises/for-each-series
Modules
import { filterSeries } from '@promises/-all';
Or import only the module you need
import { default as forEachSeries } from '@promises/for-each-series';
Browser
<script src="https://unpkg.com/@promises/-all/bundle.umd.min.js"><script>
Or import only the module you need
<script src="https://unpkg.com/@promises/for-each-series/bundle.umd.min.js"><script>
let { forEachSeries } = P;
Example
import mapSeries from '@promises/map-series';
let array: number[] = [1, 2, 3];
let map: Promise<number[]> = mapSeries(array, (value: number, index: number, array: number[]) => {
return value * index;
});
map.then((result: number[]) => {
console.log(result) // => [0, 2, 6]
});
Modules
import { rejectSeries } from '@promises/-all/fp';
Or import only the module you need
import { default as mapParallel } from '@promises/map-parallel/fp';
Browser
<script src="https://unpkg.com/@promises/-all/fp/bundle.umd.min.js"><script>
Or import only the module you need
<script src="https://unpkg.com/@promises/every-parallel/fp/bundle.umd.min.js"><script>
let { everyParallel } = PF;
Example
import filterParallel from '@promises/filter-parallel/fp';
import mapParallel from '@promises/map-parallel/fp';
import { sleep } from '@promises/-all/fp';
let array: number[] = [1, 2, 3];
let filterOdd = filterParallel((value: number) => value % 2 !== 0)(Infinity);
let sleepSecond = sleep(1000);
Promise
.resolve(array)
.then(filterOdd)
.then(mapParallel((value: number, index: number) => value + index, void 0))
.then(sleepSecond)
.then((result: number[]) => {
console.log(result) // => [1, 4]
});
Modules
it will add all the modules in the group to Promises
import Promises from '@promises/core';
import '@promises/-all/add';
Or
import Promises from '@promises/-all/add';
Or add all modules in specific groups
import Promises from '@promises/core';
import '@promises/-series/add';
import '@promises/-rxjs/add';
Or add specific modules
import Promises from '@promises/core';
import '@promises/filter-parallel/add';
import '@promises/map-parallel/add';
import '@promises/sleep/add';
Note: for bundles add only the modules needed, because Tree-shaking can not remove the unnecessary modules
Browser
<script src="https://unpkg.com/@promises/core/bundle.umd.min.js"><script>
<script src="https://unpkg.com/@promises/-all/add/bundle.umd.min.js"><script>
let { Promises } = P;
Example
import Promises from '@promises/-all/add';
let array: number[] = [1, 2, 3];
let promises: Promises<number[]> = Promises.resolve(array);
let filter: Promises<number[]> = promises.filterParallel((value: number) => value % 2 !== 0);
let map: Promises<number[]> = filter.mapParallel((value: number, index: number) => value + index);
let sleep: Promises<number[]> = map.sleep(1000);
sleep.then((result: number[]) => {
console.log(result) // => [1, 4]
});
import timeout from '@promises/timeout';
function run(forEach: Function) {
let array: number[] = [3, 7, 1, 5];
console.log('before');
forEach(array, (value: number) => {
console.log(`start: ${value}`);
return timeout((resolve) => {
console.log(`end: ${value}`);
resolve();
}, value);
}).then(() => {
console.log('complete');
});
console.log('after');
}
Parallel
import forEachParallel from '@promises/for-each-parallel';
run(forEachParallel);
// => before
// => after
// => start 3
// => start 7
// => start 1
// => start 5
// => end 1
// => end 3
// => end 5
// => end 7
// => complete
Series
import forEachSeries from '@promises/for-each-series';
run(forEachSeries);
// => before
// => after
// => start 3
// => end 3
// => start 7
// => end 7
// => start 1
// => end 1
// => start 5
// => end 5
// => complete
Wrap
import forEach from '@pakal/for-each';
import wrap from '@promises/wrap';
let forEachWrap = wrap(forEach);
run(forEachWrap);
// => before
// => after
// => start 3
// => start 7
// => start 1
// => start 5
// => complete
// => end 1
// => end 3
// => end 5
// => end 7
These modules are written in typescript and available in ES5 and ES6 standard, the requirements are a global Promise (native or polyfill).
- main - commonjs module and es5 standard (index.js)
- module - es2015 module and es5 standard (index.esm.js)
- browser - bundle in umd format includes all scope dependencies in es5 standard (bundle.umd.js, bundle.umd.min.js)
- es2015 - commonjs module and es2015 standard (index.es6.js)
- typings - typescript declaration file (index.d.ts)
Copyright © 2017 Yisrael Eliav, Licensed under the MIT license.