Permalink
Browse files

step

  • Loading branch information...
dvv committed Dec 23, 2010
1 parent c53b750 commit 635111878ebce2cb9281f7a2231f96bf91dc2be0
Showing with 100 additions and 2 deletions.
  1. +1 −0 .gitignore
  2. 0 lib/querystring.js
  3. +42 −0 lib/step.js
  4. +2 −1 package.json
  5. +55 −1 tests/promise.js
View
@@ -0,0 +1 @@
+downloaded-modules
View
0 lib/querystring.js 100755 → 100644
No changes.
View
@@ -0,0 +1,42 @@
+'use strict';
+
+(function(define){
+define(function(require,exports){
+
+// Vladimir Dronnikov
+
+// inspired by creationix's Step
+
+var when = require('./promise').when;
+
+// execute sequentially functions taken from steps[]
+// each successive is fed with the result of prior
+// each function can return an immediate value, a promise, or just throw
+// in the latter case the next function will receive Error object
+// return "undefined" to full stop.
+//
+// "context" is available to each steps as "this"
+//
+exports.Step = function(context, steps) {
+ var next;
+ next = function() {
+ var fn, result;
+ if (!steps.length) {
+ return arguments[0];
+ }
+ fn = steps.shift();
+ try {
+ result = fn.apply(context, arguments);
+ if (result !== void 0) {
+ result = when(result, next, next);
+ }
+ } catch (err) {
+ next(err);
+ }
+ return result;
+ };
+ return next();
+};
+
+});
+})(typeof define!="undefined"?define:function(factory){factory(require,exports)});
View
@@ -22,7 +22,8 @@
"contributors": [
"Dean Landolt <dean@deanlandolt.com",
"Nathan Stott <nathan.stott@whiteboard-it.com>",
- "Mark Wubben <mark@novemberborn.net>"
+ "Mark Wubben <mark@novemberborn.net>",
+ "Vladimir Dronnikov <dronnikov@gmail.com>"
],
"keywords": [
"promise",
View
@@ -1,7 +1,8 @@
var assert = require("assert"),
when = require("../lib/promise").when,
whenPromise = require("../lib/promise").whenPromise,
- defer = require("../lib/promise").defer;
+ defer = require("../lib/promise").defer,
+ Step = require("../lib/step").Step;
exports.testSpeedPlainValue = function(){
for(var i = 0; i < 1000; i++){
@@ -32,6 +33,59 @@ exports.testWhenPromiseRejectHandled = function(){
}).then(null, function(){});
};
+function veryDeferred(){
+ var deferred = defer();
+ setTimeout(function(){
+ deferred.resolve(true);
+ }, 100);
+ return deferred.promise;
+}
+
+function veryDeferred(){
+ var deferred = defer();
+ setTimeout(function(){
+ deferred.resolve(true);
+ }, 100);
+ return deferred.promise;
+}
+
+exports.testStep = function(){
+ var deferred = defer();
+ Step({foo: 'bar'}, [
+ function(){
+ console.log('S1');
+ assert.ok(this.foo === 'bar');
+ return false;
+ },
+ function(result){
+ console.log('S2');
+ assert.ok(result === false);
+ this.foo = 'baz';
+ return veryDeferred();
+ },
+ function(result){
+ console.log('S3');
+ assert.ok(this.foo === 'baz');
+ assert.ok(result === true);
+ throw Error('Catchme!');
+ },
+ function(result){
+ console.log('S4');
+ assert.ok(result instanceof Error);
+ assert.ok(result.message === 'Catchme!');
+ deferred.resolve(true);
+ // return undefined;
+ },
+ function(result){
+ console.log('S5', result);
+ // Should never come here
+ deferred.reject(false);
+ assert.ok(true === false);
+ },
+ ]);
+ return deferred.promise;
+};
+
if (require.main === module)
require("patr/runner").run(exports);

0 comments on commit 6351118

Please sign in to comment.