Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Release v2.0.0 + simplify api

removed fulfilled and rejected methods.  These are trivial and can be
implimented by higher level libraries.  This now just exports the single
`promise()` function to create a new promise.
  • Loading branch information...
commit b282130db361d91a985426af4e37d3a16c4fbc8a 1 parent e963d52
@ForbesLindesay authored
View
5 .npmignore
@@ -0,0 +1,5 @@
+node_modules
+npm-debug.log
+adapter.js
+component.json
+*.min.js
View
61 Readme.md
@@ -3,6 +3,8 @@
A bare bones implementation of Promises/A intended to pass all [promise-tests](https://github.com/domenic/promise-tests) while being as small as possible.
+ This is intended to serve as a base for you to build your own promises libraries, which would typically be expected to provide all sorts of useful helpers. Promises created by this library (or any library based off of this) should be compatable with such helpers, but helpers won't ever be included in this library.
+
## Installation
Client:
@@ -13,9 +15,66 @@
$ npm install promises-a
+ Alternatively you can download it from the [downloads area](https://github.com/ForbesLindesay/promises-a/downloads) and reference it with a script tag:
+
+```html
+<script src="promises.min.js"></script>
+```
+
+ then just refer to it as the global: `promises`
+
+## Usage
+
+ Here is an example of how to create a promises function to wrap a low level api, and provide a timeout
+
+```javascript
+var promise = require('promises-a');
+function loadDataAsync(id, timeout) {
+ timeout = timeout == null ? 500 : timeout;
+ var def = promise();
+
+ callLowLevelAPI(id, function (err, res) {
+ if (err) return def.reject(err);
+ def.resolve(res);
+ });
+
+ if (timeout) {
+ setTimeout(function () {
+ def.reject(new Error('Operation Timed Out (' + timeout + 'ms)'));
+ }, timeout);
+ }
+
+ return def.promise;
+}
+```
+
+ Because the promise can only be resolved once, the rejection will be ignored if the operation is successful within the timeout. A timeout of 0 will also be treated as infinite.
+
## API
-
+### promise()
+
+ Return a new `deferred`.
+
+### deferred
+
+#### deferred#promise
+
+ Get the promise represented by the deferred. This is just an object with a function called then.
+
+#### deferred#fulfill(value)
+
+ Put the promise in a resolved state and fulfill it with the value provided.
+
+#### deferred#reject(error)
+
+ Put the promise in a resolved state and reject it with the error provided.
+
+### promise
+
+#### promise#then(callback, errback, progback)
+
+ You can call then with three optional args. The callback is called when the promise is fulfilled, the errback is called when the promise is rejected. At the moment, progback is ignored. Calls to then can be chained, and you can return a promise from the callback, which will be resolved before being passed to the chained then.
## License
View
16 adapter.js
@@ -0,0 +1,16 @@
+var promise = require('./');
+
+
+exports.pending = promise;
+
+exports.fulfilled = function (val) {
+ var def = promise();
+ def.fulfill(val);
+ return def.promise;
+};
+
+exports.rejected = function (err) {
+ var def = promise();
+ def.reject(err);
+ return def.promise;
+};
View
2  component.json
@@ -2,7 +2,7 @@
"name": "promises-a",
"repo": "ForbesLindesay/promises-a",
"description": "A bare bones implementation of Promises/A intended to pass https://github.com/domenic/promise-tests while being as small as possible.",
- "version": "1.0.0",
+ "version": "2.0.0",
"keywords": [],
"dependencies": {},
"development": {},
View
22 index.js
@@ -1,13 +1,12 @@
(function () {
- var promises = {};
- promises.pending = function () {
+ function promise() {
var resolved = false,
fulfilled = false,
val,
waiting = [];
function then(cb, eb, pb) {
- var def = promises.pending();
+ var def = promise();
function done() {
var callback = fulfilled ? cb : eb;
if (callback) {
@@ -55,21 +54,10 @@
return {promise: {then: then}, fulfill: fulfill, reject: reject};
};
-
- promises.fulfilled = function (val) {
- var def = promises.pending();
- def.fulfill(val);
- return def.promise;
- };
-
- promises.rejected = function (err) {
- var def = promises.pending();
- def.reject(err);
- return def.promise;
- };
+
if (typeof module != 'undefined' && typeof module.exports != 'undefined') {
- module.exports = promises;
+ module.exports = promise;
} else {
- window.promises = promises;
+ window.promise = promise;
}
}());
View
4 package.json
@@ -1,10 +1,10 @@
{
"name": "promises-a",
- "version": "1.0.0",
+ "version": "2.0.0",
"description": "A bare bones implementation of Promises/A intended to pass https://github.com/domenic/promise-tests while being as small as possible",
"main": "index.js",
"scripts": {
- "test": "promise-tests all index.js && uglifyjs index.js > index.min.js"
+ "test": "promise-tests all adapter.js && uglifyjs index.js > index.min.js"
},
"repository": {
"type": "git",
Please sign in to comment.
Something went wrong with that request. Please try again.