-
-
Save tlrobinson/293552 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// EXAMPLE 2: async response | |
// AJSGI using Q promise manager | |
var app = function (env) { | |
var response = Q.defer(); | |
setTimeout(function () { | |
response.resolve({ status: 200, headers: {}, body: ["foobar"] }); | |
}, 1000); | |
return response.promise; | |
}; | |
// EJSGI (or AJSGI using EJSGI-like helper stream/prommise combo object) | |
var app = function (env) { | |
var p = new Promise; | |
var s = new (env.jsgi.stream); | |
setTimeout(function () { | |
s.write("foobar"); | |
s.close(); | |
p.emitSuccess({ status: 200, headers: {}, body: s }); | |
}, 1000); | |
return p; | |
}; | |
// PSGI equivalent | |
var app = function(env) { | |
return function(respond) { | |
setTimeout(function() { | |
respond({ status: 200, headers: {}, body: ["foobar"] }); | |
}, 1000); | |
} | |
}; | |
// AJSGI using "then" duck typed promises manually | |
var app = function(env) { | |
return { then : function(respond) { | |
setTimeout(function() { | |
respond({ status: 200, headers: {}, body: ["foobar"] }); | |
}, 1000); | |
}}; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// EXAMPLE 3: sync response + async streaming | |
// AJSGI using Q promise manager | |
var app = function (env) { | |
var response = Q.defer(); | |
return { status: 200, headers: {}, body: { | |
forEach: function (write) { | |
var close = Q.defer(); | |
setTimeout(function() { write("foo"); }, 1000); | |
setTimeout(function() { write("bar"); close.resolve(); }, 2000); | |
return close.promise; | |
} | |
}}; | |
}; | |
// EJSGI (or AJSGI using EJSGI-like helper stream/prommise combo object) | |
var app = function (env) { | |
var s = new (env.jsgi.stream); | |
setTimeout(function () { s.write("foo"); }, 1000); | |
setTimeout(function () { s.write("bar"); s.close(); }, 2000); | |
return { status: 200, headers: {}, body: s }; | |
}; | |
// PSGI equivalent | |
var app = function(env) { | |
return function(respond) { | |
var s = respond({ status: 200, headers: {} }); | |
setTimeout(function() { s.write("foo"); }, 1000); | |
setTimeout(function() { s.write("bar"); s.close(); }, 2000); | |
}; | |
}; | |
// AJSGI manually using "then" duck typed promises | |
var app = function(env) { | |
return { status: 200, headers: {}, body: { | |
forEach : function(write) { | |
return { then : function(close) { | |
setTimeout(function() { write("foo"); }, 1000); | |
setTimeout(function() { write("bar"); close(); }, 2000); | |
}}; | |
} | |
}}; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// EXAMPLE 4: async response + streaming body async | |
// AJSGI using Q promise manager | |
var app = function (env) { | |
var response = Q.defer(); | |
setTimeout(function() { | |
response.resolve({ status: 200, headers: {}, body: { | |
"forEach": function (write) { | |
var close = Q.defer(); | |
setTimeout(function() { write("foo"); }, 1000); | |
setTimeout(function() { write("bar"); close.resolve(); }, 2000); | |
return close.promise; | |
} | |
}}); | |
}, 1000); | |
return response.promise; | |
}; | |
// EJSGI (or AJSGI using EJSGI-like helper stream/prommise combo object) | |
var app = function (env) { | |
var p = new Promise; | |
setTimeout(function() { | |
var s = new (env.jsgi.stream); | |
p.emitSuccess({ status: 200, headers: {}, body: s }); | |
setTimeout(function () { s.write("foo"); }, 1000); | |
setTimeout(function () { s.write("bar"); s.close(); }, 2000); | |
}, 1000); | |
return p; | |
}; | |
// PSGI equivalent | |
var app = function(env) { | |
return function(respond) { | |
setTimeout(function() { | |
var s = respond({ status: 200, headers: {} }); | |
setTimeout(function() { s.write("foo"); }, 1000); | |
setTimeout(function() { s.write("bar"); w.close(); }, 2000); | |
}, 1000); | |
}; | |
}; | |
// AJSGI manually using "then" duck typed promises | |
var app = function(env) { | |
return { then : function(respond) { | |
setTimeout(function() { | |
respond({ status: 200, headers: {}, body: { | |
"forEach" : function(write) { | |
return { then : function(close) { | |
setTimeout(function() { write("foo"); }, 1000); | |
setTimeout(function() { write("bar"); close(); }, 2000); | |
}}; | |
} | |
}}); | |
}, 1000) | |
}}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment