What is yaku?
Yaku is a lightweight and fast Promise library that is fully compatible with ES6 Promises. It aims to provide better performance and smaller size compared to native Promises and other Promise libraries.
What are yaku's main functionalities?
Basic Promise Usage
This demonstrates the basic usage of Yaku to create a new Promise and handle its resolution.
const Yaku = require('yaku');
const promise = new Yaku((resolve, reject) => {
setTimeout(() => resolve('Hello, Yaku!'), 1000);
});
promise.then(value => console.log(value));
Chaining Promises
This demonstrates how to chain multiple `then` calls to handle the resolved value step by step.
const Yaku = require('yaku');
const promise = new Yaku((resolve, reject) => {
setTimeout(() => resolve(1), 1000);
});
promise
.then(value => value + 1)
.then(value => value * 2)
.then(value => console.log(value));
Error Handling
This demonstrates how to handle errors in Yaku Promises using the `catch` method.
const Yaku = require('yaku');
const promise = new Yaku((resolve, reject) => {
setTimeout(() => reject(new Error('Something went wrong')), 1000);
});
promise
.then(value => console.log(value))
.catch(error => console.error(error.message));
Promise.all
This demonstrates how to use `Yaku.all` to wait for multiple Promises to resolve.
const Yaku = require('yaku');
const promise1 = Yaku.resolve(1);
const promise2 = Yaku.resolve(2);
const promise3 = Yaku.resolve(3);
Yaku.all([promise1, promise2, promise3])
.then(values => console.log(values));
Other packages similar to yaku
bluebird
Bluebird is a fully featured Promise library with a focus on performance and additional features not found in native Promises. It offers more utility methods and better error handling compared to Yaku, but it is larger in size.
q
Q is another Promise library that provides a lot of additional functionality such as deferred objects and more advanced control flow. It is more feature-rich compared to Yaku but also comes with a larger footprint.
when
When is a lightweight Promise library that focuses on performance and small size, similar to Yaku. It provides a few more utilities for working with Promises but is generally comparable in terms of performance and size.
Overview
The src/yaku.coffee
of Yaku is full compatible with V8's native Promise, but much faster.
If you want to learn how Promise works, read the minimum implementation docs/minPromiseA+.coffee. Without comments, it is only 80 lines of code.
It only implements the constructor
and then
. It passed all the tests of promises-aplus-tests.
I am not a optimization freak, I try to keep the source code readable and maintainable.
Features
- The minified file is only 2.7KB (Bluebird / 73KB, ES6-promise / 18KB)
- 100% compliant with Promise/A+ specs
- Better performance than the native Promise
- Works on IE5+ and other major browsers
- Possibly unhandled rejection support
Quick Start
Node.js
npm install yaku
Then:
Promise = require 'yaku'
Browser
Download the yaku.js
file from release page. It supports both AMD
and CMD
.
Raw usage without AMD
or CMD
:
<script type="text/javascript" src ="yaku.js"></script>
<script>
Promise = Yaku
</script>
Compare
These comparisons only reflect some limited truth, no one is better than than others on all aspects.
iojs v1.8.1
OS darwin
Arch x64
CPU Intel(R) Core(TM) i7-4850HQ CPU @ 2.30GHz
Name | Unit Test | 1ms async task | sync task | Helpers | file size |
---|
Yaku | 872/872 | 283ms | 68ms | ++ | 2.7KB |
Bluebird | 872/872 | 244ms | 164ms | +++++++ | 73KB |
ES6-promise | 872/872 | 435ms | 110ms | + | 18KB |
native | 872/872 | 816ms | 605ms | + | 0KB |
q | 208/872 | 2637ms | 2327ms | +++ | 24K |
- Helpers: extra methods that help with your promise programming, such as
async flow control helpers, debug helpers.
- 1ms async task:
npm run no -- benchmark
, the smaller the better. - sync task:
npm run no -- benchmark --sync
, the smaller the better.
API
FAQ
Unit Test
This project use promises-aplus-tests to test the compliance of Promise/A+ specification. There are about 900 test cases.
Use npm run no -- test
to run the unit test.
Benchmark
Use npm run no -- benchmark
to run the benchmark.
Misc.
The name yaku
comes from the word 約束(yakusoku)
which means promise.