ysmood / yaku

A lightweight promise library
https://tonicdev.com/ysmood/yaku
MIT License
290 stars 28 forks source link
es6-promise lightweight promise promises

<img src="http://promisesaplus.com/assets/logo-small.png" alt="Promises/A+ logo" title="Promises/A+ 1.1 compliant" align="right" />

Overview

Yaku is full compatible with ES6's native Promise, but much faster, and more error friendly. If you want to learn how Promise works, read the minimum implementation yaku.aplus. Without comments, it is only 80 lines of code (gzipped size is 0.5KB). It only implements the constructor and then.

Yaku passed all the tests of promises-aplus-tests, promises-es6-tests, and even the core-js tests.

I am not an optimization freak, I try to keep the source code readable and maintainable. I write this lib to research one of my data structure ideas: docs/lazyTree.md.

NPM version Build Status Coverage Status

Features

Quick Start

Node.js

npm install yaku

Then:

var Promise = require('yaku');

Or if you don't want any extra debug helper, ES6 only version is here:

var Promise = require('yaku/lib/yaku.core');

Or if you only want aplus support:

var Promise = require('yaku/lib/yaku.aplus');

Browser

Raw usage:

<script type="text/javascript" src="https://raw.githubusercontent.com/ysmood/yaku/master/src/yaku.js"></script>
<script>
    // Yaku will be assigned to `window.Yaku`.
    var Promise = Yaku;
</script>

Change Log

docs/changelog.md

Compare to Other Promise Libs

These comparisons only reflect some limited truth, no one is better than all others on all aspects. There are tons of Promises/A+ implementations, you can see them here. Only some of the famous ones were tested.

You can reproduce it on your own machine with npm run no -- benchmark.

Date: Wed Aug 15 2018 23:06:02 GMT+0900 (Japan Standard Time)
Node v10.8.0
OS   darwin
Arch x64
CPU  Intel(R) Core(TM) i7-4850HQ CPU @ 2.30GHz
name unit tests coverage 1ms async task optional helpers helpers gzip
yaku@0.19.1 100% 100% 274ms / 112MB 35 1.9KB
yaku.core@0.19.1 100% 100% 292ms / 110MB 29 1.6KB
yaku.aplus@0.19.1 x (91 failed) 100% 100% 328ms / 126MB 7 0.5KB
bluebird@3.5.1 x (33 failed) 99% 96% 183ms / 88MB partial 103 16.1KB
es6-promise@4.2.4 x (48 failed) ? ? 510ms / 106MB x 13 2.4KB
pinkie@2.0.4 x (45 failed) ? ? 284ms / 138MB v 10 1.2KB
native@10.8.0 ? ? 133ms / 86MB x 11 0KB
core-js@2.5.7 x (5 failed) ? ? 239ms / 99MB x 13 4.8KB
es6-shim@0.35.3 ? ? 161ms / 87MB x 11 15.6KB
q@1.5.1 x (21 failed) ? ? 896ms / 326MB x 74 4.7KB
my-promise@1.1.0 x (10 failed) ? ? 583ms / 224MB x 10 4KB

FAQ

Unhandled Rejection

Yaku will report any unhandled rejection via console.error by default, in case you forget to write catch. You can catch them manually:

For more spec read Unhandled Rejection Tracking Browser Events.

API


Utils

It's a bundle of all the following functions. You can require them all with var yutils = require("yaku/lib/utils"), or require them separately like require("yaku/lib/flow"). If you want to use it in the browser, you have to use browserify or webpack. You can even use another Promise lib, such as:

require("yaku/lib/_").Promise = require("bluebird");
var source = require("yaku/lib/source");

// now "source" use bluebird instead of yaku.

Observable

Unit Test

This project use promises-aplus-tests to test the compliance of Promises/A+ specification. There are about 900 test cases.

Use npm run no -- test to run the unit test against yaku.

Test other libs

basic test

To test bluebird: npm run no -- test-basic --shim bluebird

The bluebird can be replaced with other lib, see the test/getPromise.js for which libs are supported.

aplus test

To test bluebird: npm run no -- test-aplus --shim bluebird

The bluebird can be replaced with other lib, see the test/getPromise.js for which libs are supported.

es6 test

To test bluebird: npm run no -- test-es6 --shim bluebird

The bluebird can be replaced with other lib, see the test/getPromise.js for which libs are supported.

Benchmark

Use npm run no -- benchmark to run the benchmark.

async/await generator wrapper

Node v5.6.0
OS   darwin
Arch x64
CPU  Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz

yaku: 117ms
co: 283ms
bluebird: 643ms

Contribution

Make sure you have npm and npm install at the root of the project first.

Other than use gulp, all my projects use nokit to deal with automation. Run npm run no -- -h to print all the tasks that you can use.

Update readme.md

Please don't alter the readme.md directly, it is compiled from the docs/readme.jst.md. Edit the docs/readme.jst.md and execute npm run no to rebuild the project.