Promise utilities for async/await-ready environment.
npm install villa


Villa is a set of promise utilities for async-await-ready environment.
Promises have been widely used in JavaScript, and there are quite a few fully
featured promise libraries like
bluebird and
Q. But with the growing adoption ofasync/await provided by ES-next (via transpilers like
TypeScript and Babel),
some critical features provided by those libraries become less relevant.
And there is another problem with third-party promise for code usingasync/await: it could be confusing having different promise instances with
different APIs, while an async function always returns native promise object.
While most of the promise use cases so far can be addressed usingasync/await with simple helpers, I created villa with my favorite features
from my own promise library ThenFail and
more.
Villa is written in TypeScript and compiled with TypeScript 2.0, and works with
TypeScript, Babel and ES6 generators.
``sh`
npm install villa --save
ts
import * as FS from 'fs';
import * as Path from 'path';import * as v from 'villa';
// Add support for Node.js specific features, e.g. awaitable for Node.js objects.
import 'villa/platform/node';
async function copy(source, target) {
let readStream = FS.createReadStream(source);
let writeStream = FS.createWriteStream(target);
readStream.pipe(writeStream);
await v.awaitable(writeStream, [readStream]);
}
async function copyAll(sourceDir, targetDir) {
await v
.chainable(v.call(FS.readdir, sourceDir))
.filter(async fileName => {
let stats = await v.call(FS.stat, fileName);
return stats.isFile();
})
.each(async fileName => {
let source = Path.join(sourceDir, fileName);
let target = Path.join(targetDir, fileName);
await copy(source, target);
});
}
`API References
#### [[+]](src/awaitable.ts#L17)
awaitableCreate a promise for an object.
#### [[+]](src/chainable.ts#L124)
chainableWrap given resolvable with a chainable derived of built-in promise.
#### [[+]](src/concurrency.ts#L18)
lockA simple asynchronous lock that helps queueing operations.
#### [[+]](src/concurrency.ts#L36)
parallelRun tasks in parallel, similar to
v.map but not mean to transform.#### [[+]](src/concurrency.ts#L53)
raceRace tasks and fulfill or reject as soon as one of them fulfills or rejects.
#### [[+]](src/function.ts#L21)
callCall a Node.js-style asynchronous function and return a correspondent
promise.
#### [[+]](src/function.ts#L42)
asyncWrap a Node.js-style asynchronous function to a function that returns
promise.
#### [[+]](src/miscellaneous.ts#L6)
bear(_error: any): undefinedA no-operation function that acts as the rejection handler of a promise.
#### [[+]](src/miscellaneous.ts#L14)
sleep(duration: number): PromiseCreate a promise that will be fulfilled in given duration (milliseconds).
#### [[+]](src/miscellaneous.ts#L36)
retryRetry procedure in the handler for several times.
#### [[+]](src/array.ts#L12)
eachAsynchronous version of
Array#forEach().#### [[+]](src/array.ts#L34)
someAsynchronous version of
Array#some().#### [[+]](src/array.ts#L56)
everyAsynchronous version of
Array#every().#### [[+]](src/array.ts#L78)
mapAsynchronous version of
Array#map() with basic concurrency control.#### [[+]](src/array.ts#L150)
reduce+1Asynchronous version of
Array#reduce().##### Overloads:
-
reduce#### [[+]](src/array.ts#L175)
reduceRight+1Asynchronous version of
Array#reduceRight().##### Overloads:
-
reduceRight#### [[+]](src/array.ts#L206)
filterAsynchronous version of
Array#filter().#### [[+]](src/array.ts#L231)
findAsynchronous version of
Array#find().#### [[+]](src/array.ts#L248)
findIndexAsynchronous version of
Array#findIndex()`.MIT License.