Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add readme

  • Loading branch information...
commit b8c1a4ff675e20e5c5bcb0f2f7c3cf0843097854 1 parent b43e040
@waka authored
View
0  README.md
No changes.
View
86 promise-simple.js
@@ -4,6 +4,12 @@
*/
(function(define) {
define([], function() {
+
+ 'use strict';
+
+ // Use freeze if exists.
+ var freeze = Object.freeze || function() {};
+
/**
* Interface Promise/A.
@@ -46,28 +52,33 @@ define([], function() {
Deferred.prototype.state_;
/**
- * @type {Array.<>}
+ * @type {!Array.<!Array>}
* @private
*/
Deferred.prototype.chain_;
/**
- * @type {*}
+ * @type {Object}
* @private
*/
- Deferred.prototype.result_;
+ Deferred.prototype.scope_;
/**
- * @type {Object}
+ * The current Deferred result.
+ * @type {*}
* @private
*/
- Deferred.prototype.scope_;
+ Deferred.prototype.result_;
/**
* @override
*/
- Deferred.prototype.then = function(callback, errback) {
- var deferred = new Deferred(this.scope_);
+ Deferred.prototype.then = function(callback, errback, progback) {
+ this.chain_.push([callback || null, errback || null, progback || null]);
+ if (this.state_ !== Deferred.State.UNRESOLVED) {
+ this.fire_();
+ }
+ return this;
};
/**
@@ -75,6 +86,7 @@ define([], function() {
*/
Deferred.prototype.resolve = function(value) {
this.state_ = Deferred.State.RESOLVED;
+ this.fire_(value);
};
/**
@@ -82,8 +94,44 @@ define([], function() {
*/
Deferred.prototype.reject = function(error) {
this.state_ = Deferred.State.REJECTED;
+ this.fire_(error);
};
+ /**
+ * @return {boolean}
+ */
+ Deferred.prototype.isResolved = function() {
+ return this.state_ === Deferred.State.RESOLVED;
+ };
+
+ /**
+ * @return {boolean}
+ */
+ Deferred.prototype.isRejected = function() {
+ return this.state_ === Deferred.State.REJECTED;
+ };
+
+ /**
+ * @param {*} value
+ * @private
+ */
+ Deferred.prototype.fire_ = function(value) {
+ var res = (typeof value !== 'undefined') ? value : this.result_;
+
+ while(this.chain_.length) {
+ var entry = this.chain_.shift();
+ var fn = (this.state_ === Deferred.State.REJECTED) ? entry[1] : entry[0];
+ if (fn) {
+ try {
+ this.result_ = res = fn.call(this.scope_, res);
+ } catch (e) {
+ this.state_ = Deferred.State.REJECTED;
+ this.result_ = res = e;
+ }
+ }
+ }
+ }
+
/**
* @type {enum}
@@ -93,22 +141,28 @@ define([], function() {
RESOLVED: 'resolved',
REJECTED: 'rejected'
};
+ freeze(Deferred.State);
/**
- * @param {Array.<Function|Deferred>|Deferred|Function} args
+ * @param {...*} var_args
* @return {Deferred}
* @static
*/
- Deferred.when = function(args) {
+ Deferred.when = function(var_args) {
var d = new Deferred();
- if (args instanceof Array) {
- } else if (args instanceof Deferred) {
- } else if (args instanceof Function) {
- } else {
- throw Error('Illegal arguments');
- }
+ Array.prototype.slice(arguments).forEach(function(arg) {
+ if (arg instanceof Deferred) {
+ d = d.chainDeferred(arg);
+ } else if (arg instanceof Function) {
+ d = d.then(arg);
+ } else {
+ d = d.then(function(res) {
+ return arg;
+ });
+ }
+ });
return d;
};
@@ -124,5 +178,5 @@ define([], function() {
// If no define or module, attach to current context.
typeof module !== 'undefined' ?
function(deps, factory) { module.exports = factory(); } :
- function(deps, factory) { this.when = factory(); }
+ function(deps, factory) { this.Deferred = factory(); }
);
View
18 test/node-promise-simple-test.js
@@ -0,0 +1,18 @@
+var assert = require('assert');
+var Deferred = require('../promise-simple');
+
+
+function test() {
+ var cnt = 0;
+
+ var d = new Deferred();
+ d.then(function() {
+ cnt++;
+ assert.equal(cnt, 3);
+ });
+ cnt++;
+ assert.equal(cnt, 1);
+ d.resolve();
+};
+
+test();
View
33 test/promise-simple-test.html
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta charset="utf-8" />
+ <title>js-primise-simple test</title>
+ <script src="../promise-simple.js"></script>
+ </head>
+ <body>
+ <script>
+ var d = new Deferred();
+ d.then(function() {
+ console.log("ok");
+ return "hoge";
+ })
+ .then(function(res) {
+ console.log(res);
+ throw Error("bra");
+ })
+ .then(null,
+ function(err) {
+ console.log(err.message);
+ });
+ d.resolve();
+
+ var d2 = new Deferred();
+ d2.then(null,
+ function(err) {
+ console.log(err.message);
+ });
+ d2.reject(new Error("has error"));
+ </script>
+ </body>
+</html>
Please sign in to comment.
Something went wrong with that request. Please try again.