Comparing version 0.0.1 to 0.0.2
{ | ||
"name": "yaku", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "A pure ES6 Promise/A+ that doesn't hurts.", | ||
"main": "dist/main.js", | ||
"scripts": { | ||
"test": "npm test" | ||
"no": "no", | ||
"test": "no test" | ||
}, | ||
@@ -28,6 +29,11 @@ "repository": { | ||
"devDependencies": { | ||
"coffee-script": "^1.9.1", | ||
"coffeelint": "^1.8.1", | ||
"nokit": "^0.5.4" | ||
"bluebird": "2.9.25", | ||
"coffee-script": "1.9.2", | ||
"coffeelint": "1.9.4", | ||
"es6-promise": "2.1.1", | ||
"nokit": "0.6.2", | ||
"promises-aplus-tests": "*", | ||
"q": "1.3.0", | ||
"uglify-js": "2.4.20" | ||
} | ||
} |
239
readme.md
@@ -1,1 +0,238 @@ | ||
# Under Development | ||
# Overview | ||
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 | ||
## 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 | ||
- ### **[constructor(executor)](src/yaku.coffee?source#L23)** | ||
This class follows the [Promises/A+](https://promisesaplus.com) and | ||
[ES6](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-promise-objects) spec | ||
with some extra helpers. | ||
- **<u>param</u>**: `executor` { _Function_ } | ||
Function object with two arguments resolve and reject. | ||
The first argument fulfills the promise, the second argument rejects it. | ||
We can call these functions, once our operation is completed. | ||
- **<u>example</u>**: | ||
```coffee | ||
Promise = require 'yaku' | ||
p = new Promise (resolve, reject) -> | ||
setTimeout -> | ||
if Math.random() > 0.5 | ||
resolve 'ok' | ||
else | ||
reject 'no' | ||
``` | ||
- ### **[then(onFulfilled, onRejected)](src/yaku.coffee?source#L42)** | ||
Appends fulfillment and rejection handlers to the promise, | ||
and returns a new promise resolving to the return value of the called handler. | ||
- **<u>param</u>**: `onFulfilled` { _Function_ } | ||
Optional. Called when the Promise is resolved. | ||
- **<u>param</u>**: `onRejected` { _Function_ } | ||
Optional. Called when the Promise is rejected. | ||
- **<u>return</u>**: { _Yaku_ } | ||
It will return a new Yaku which will resolve or reject after | ||
the current Promise. | ||
```coffee | ||
Promise = require 'yaku' | ||
p = Promise.resolve 10 | ||
p.then (v) -> | ||
console.log v | ||
``` | ||
- ### **[catch(onRejected)](src/yaku.coffee?source#L59)** | ||
The catch() method returns a Promise and deals with rejected cases only. | ||
It behaves the same as calling `Promise.prototype.then(undefined, onRejected)`. | ||
- **<u>param</u>**: `onRejected` { _Function_ } | ||
A Function called when the Promise is rejected. | ||
This function has one argument, the rejection reason. | ||
- **<u>return</u>**: { _Yaku_ } | ||
A Promise that deals with rejected cases only. | ||
```coffee | ||
Promise = require 'yaku' | ||
p = Promise.reject 10 | ||
p.catch (v) -> | ||
console.log v | ||
``` | ||
- ### **[@resolve(value)](src/yaku.coffee?source#L74)** | ||
The Promise. resolve(value) method returns a Promise object that is resolved with the given value. | ||
If the value is a thenable (i.e. has a then method), the returned promise will "follow" that thenable, | ||
adopting its eventual state; otherwise the returned promise will be fulfilled with the value. | ||
- **<u>param</u>**: `value` { _Any_ } | ||
Argument to be resolved by this Promise. | ||
Can also be a Promise or a thenable to resolve. | ||
- **<u>return</u>**: { _Yaku_ } | ||
```coffee | ||
Promise = require 'yaku' | ||
p = Promise.resolve 10 | ||
``` | ||
- ### **[@reject(reason)](src/yaku.coffee?source#L87)** | ||
The Promise.reject(reason) method returns a Promise object that is rejected with the given reason. | ||
- **<u>param</u>**: `reason` { _Any_ } | ||
Reason why this Promise rejected. | ||
- **<u>return</u>**: { _Yaku_ } | ||
```coffee | ||
Promise = require 'yaku' | ||
p = Promise.reject 10 | ||
``` | ||
- ### **[@race(iterable)](src/yaku.coffee?source#L108)** | ||
The Promise.race(iterable) method returns a promise that resolves or rejects | ||
as soon as one of the promises in the iterable resolves or rejects, | ||
with the value or reason from that promise. | ||
- **<u>param</u>**: `iterable` { _iterable_ } | ||
An iterable object, such as an Array. | ||
- **<u>return</u>**: { _Yaku_ } | ||
The race function returns a Promise that is settled | ||
the same way as the first passed promise to settle. | ||
It resolves or rejects, whichever happens first. | ||
```coffee | ||
Promise = require 'yaku' | ||
Promise.race [ | ||
123 | ||
Promise.resolve 0 | ||
] | ||
.then (value) -> | ||
console.log value # => 123 | ||
``` | ||
- ### **[@all(iterable)](src/yaku.coffee?source#L136)** | ||
The `Promise.all(iterable)` method returns a promise that resolves when | ||
all of the promises in the iterable argument have resolved. | ||
The result is passed as an array of values from all the promises. | ||
If something passed in the iterable array is not a promise, | ||
it's converted to one by Promise.resolve. If any of the passed in promises rejects, | ||
the all Promise immediately rejects with the value of the promise that rejected, | ||
discarding all the other promises whether or not they have resolved. | ||
- **<u>param</u>**: `iterable` { _iterable_ } | ||
An iterable object, such as an Array. | ||
- **<u>return</u>**: { _Yaku_ } | ||
```coffee | ||
Promise = require 'yaku' | ||
Promise.all [ | ||
123 | ||
Promise.resolve 0 | ||
] | ||
.then (values) -> | ||
console.log values # => [123, 0] | ||
``` | ||
- ### **[@onUnhandledRejection(reason)](src/yaku.coffee?source#L173)** | ||
Catch all possibly unhandled rejections. | ||
If it is set, auto `console.error` unhandled rejection will be disabed. | ||
- **<u>param</u>**: `reason` { _Any_ } | ||
The rejection reason. | ||
```coffee | ||
Promise = require 'yaku' | ||
Promise.onUnhandledRejection = (reason) -> | ||
console.error reason | ||
``` | ||
# FAQ | ||
- Long stack trace support? | ||
> Latest Node.js and browsers are already support it. If you enabled it, Yaku will take advantage of it | ||
> without much overhead. Such as this library [longjohn][] for Node.js, or this article for [Chrome][crhome-lst]. | ||
# 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. | ||
[docs/minPromiseA+.coffee]: docs/minPromiseA+.coffee | ||
[promises-aplus-tests]: https://github.com/promises-aplus/promises-tests | ||
[longjohn]: https://github.com/mattinsler/longjohn | ||
[crhome-lst]: http://www.html5rocks.com/en/tutorials/developertools/async-call-stack |
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
Empty package
Supply chain riskPackage does not contain any code. It may be removed, is name squatting, or the result of a faulty package publish.
Found 1 instance in 1 package
12336
5
238
8
2