Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/loveky/yapi.js
yet another Promises/A+ implementation which works in both browser and node
https://github.com/loveky/yapi.js
Last synced: 12 days ago
JSON representation
yet another Promises/A+ implementation which works in both browser and node
- Host: GitHub
- URL: https://github.com/loveky/yapi.js
- Owner: loveky
- Created: 2015-03-17T09:55:06.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-03-24T13:06:44.000Z (over 9 years ago)
- Last Synced: 2024-10-07T04:03:36.924Z (about 1 month ago)
- Language: JavaScript
- Homepage:
- Size: 195 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# YAPI.js [![Build Status](https://travis-ci.org/loveky/yapi.js.svg?branch=master)](https://travis-ci.org/loveky/yapi.js)
yet another [Promises/A+](https://promisesaplus.com/) implementation which works in both browser and node## Install
### Bower
`bower install -S yapi`### NPM
`npm install --save yapi`## Usage
You can use two ways to create a promsie.### Option 1
Use `YAPI.createPromsie` method.
```javascript
var promise = YAPI.createPromise(function (resolve, reject) {
// if promise is fulfilled
resolve();
// or if promise is rejected
reject();
});promise.then(function () {
// fulfilled callback
}, function () {
// rejected callback
});
```
Here is an example of a simple XHR2 wrapper written using YAPI.js:
```javascript
var getJSON = function(url) {
var promise = YAPI.createPromise(function(resolve, reject){
var client = new XMLHttpRequest();
client.open("GET", url);
client.onreadystatechange = handler;
client.responseType = "json";
client.setRequestHeader("Accept", "application/json");
client.send();function handler() {
if (this.readyState === this.DONE) {
if (this.status === 200) { resolve(this.response); }
else { reject(this); }
}
};
});return promise;
};
```
Check the [example.html](https://github.com/loveky/yapi.js/blob/master/example.html) for a full demo which uses the about XHR2 wrapper.### Option 2
Manualy create a deferred object and resolve/reject it ondemand.
```javascript
var getUserInfo = function () {
var deferred = YAPI.defer();
// fetch user information throught AJAX
deferred.resolve(); // if AJAX request succeed
// or
deferred.reject(); // if something wrong
return deferred.promise;
};
```## Develop
```shell
npm test # run Promises/A+ Compliance Test Suite
grunt build # jshint & generate min version
```## Todos
- [ ] refine readme
- [ ] add usage information
- [x] add Promise/A+ logo after pass [Promises/A+ Compliance Test Suite](https://github.com/promises-aplus/promises-tests)
- [x] provide minimized version
- [ ] provide `YAPI.all` method
- [ ] provide `promise.finally`
- [ ] provide `promise.catch`
- [x] test against [Promises/A+ Compliance Test Suite](https://github.com/promises-aplus/promises-tests)