Permalink
Browse files

promises with express

  • Loading branch information...
dominykas committed Oct 1, 2012
1 parent 3a2a3fc commit cb039c73b1a5cdce5354e3bcc470a6f5def64514
Showing 303 changed files with 19,440 additions and 0 deletions.
View
@@ -0,0 +1,49 @@
+if (typeof define !== 'function') { var define = require('amdefine')(module); }
+
+define(["express", "q"], function(express, Q) {
+
+ var app = express();
+ app.use(express.static("./testdata"));
+ app.use(express.logger());
+
+ var makeMiddleware = function(msg, timeout)
+ {
+ return function(req, res, next){
+
+ console.log("Creating", msg);
+
+ res.promises = res.promises || [];
+
+ var deferred = Q.defer();
+
+ res.promises.push(deferred.promise);
+
+ setTimeout(function(){
+
+ console.log("Resolving", msg);
+ deferred.resolve();
+
+ }, timeout);
+
+ next();
+
+ }
+ };
+
+ app.use("/", makeMiddleware("load users", 100));
+ app.use("/", makeMiddleware("load products", 200));
+
+ app.get("/", function(req, res){
+
+ console.log("Waiting for promises");
+ Q.all(res.promises).then(function(){
+ console.log("Done!");
+ res.send("OK");
+ }).end();
+
+ });
+
+
+ app.listen(1337);
+
+});

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit cb039c7

Please sign in to comment.