Permalink
Browse files

Initial Commit

Passes all tests
  • Loading branch information...
0 parents commit 1581587cf19bd81d8b050ffff3ce1f933839fcd2 @ForbesLindesay ForbesLindesay committed Oct 31, 2012
Showing with 150 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +22 −0 Readme.md
  3. +12 −0 component.json
  4. +90 −0 index.js
  5. +22 −0 package.json
@@ -0,0 +1,4 @@
+components
+build
+node_modules
+*.min.js
@@ -0,0 +1,22 @@
+
+# promises-a
+
+ 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.
+
+## Installation
+
+ Client:
+
+ $ component install ForbesLindesay/promises-a
+
+ Server:
+
+ $ npm install promises-a
+
+## API
+
+
+
+## License
+
+ MIT
@@ -0,0 +1,12 @@
+{
+ "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": "0.0.1",
+ "keywords": [],
+ "dependencies": {},
+ "development": {},
+ "scripts": [
+ "index.js"
+ ]
+}
@@ -0,0 +1,90 @@
+(function () {
+ var promises = {};
+ promises.pending = function () {
+ var resolved = false,
+ fulfilled = false,
+ val,
+ rejectors = [], acceptors = [];
+
+ function then(cb, eb, pb) {
+ var def = promises.pending();
+ function fulfill(val) {
+ setTimeout(function () {
+ if (cb) {
+ try {
+ val = cb(val);
+ } catch (ex) {
+ return def.reject(ex);
+ }
+ }
+ def.fulfill(val);
+ }, 0);
+ }
+ function reject(err) {
+ setTimeout(function () {
+ var val;
+ if (eb) {
+ try {
+ val = eb(err);
+ } catch (ex) {
+ return def.reject(ex);
+ }
+ def.fulfill(val);
+ } else {
+ def.reject(err);
+ }
+ }, 0);
+ }
+ if (resolved) {
+ if (fulfilled) {
+ fulfill(val);
+ } else {
+ reject(val);
+ }
+ } else {
+ acceptors.push(fulfill);
+ rejectors.push(reject);
+ }
+ return def.promise;
+ }
+ function resolve(success, value) {
+ if (resolved) return;
+ if (success && typeof value === 'object' && typeof value.then === 'function') {
+ value.then(fulfill, reject);
+ return;
+ }
+ resolved = true;
+ fulfilled = success;
+ val = value;
+ var cbs = success ? acceptors : rejectors;
+ for (var i = 0; i < cbs.length; i++) {
+ cbs[i](val);
+ }
+ }
+ function fulfill(val) {
+ resolve(true, val);
+ }
+ function reject(err) {
+ resolve(false, err);
+ }
+
+ 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;
+ } else {
+ window.promises = promises;
+ }
+}());
@@ -0,0 +1,22 @@
+{
+ "name": "promises-a",
+ "version": "0.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"
+ },
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/ForbesLindesay/promises-a.git"
+ },
+ "keywords": [
+ "promises",
+ "promise"
+ ],
+ "author": "ForbesLindesay",
+ "license": "MIT",
+ "devDependencies": {
+ "promise-tests": "~2.0.0"
+ }
+}

0 comments on commit 1581587

Please sign in to comment.