Skip to content

Instantly share code, notes, and snippets.

View kriszyp's full-sized avatar
💭
Working on @HarperDB

Kris Zyp kriszyp

💭
Working on @HarperDB
View GitHub Profile
// new syntax
function addOne(value){
return (when <: value) + 1;
}
// as effectively sugar for:
function addOne(value){
return when(value, function(value){
return value + 1;
});
}
return [200, headers:{}, body:{
forEach: function(write){
var promise = new Promise;
someEventEmitter.addListener("data", function(data){
write(data);
}
someEventEmitter.addListener("complete", function(){
promise.resolve();
}
return promise;
require("./jsgi-node").start(function(request){
var requestBody = request.input.read().decodeToString();
return {
status:200,
headers:{},
body:["echo: " + requestBody]
};
});
XMLHttpRequest = require("browser/xhr").XMLHttpRequest;
var request = require("jsgi-client").request;
var when = require("promise").when;
var queue = require("event-queue");
when(request({
method: "GET",
url: "http://google.com/"
}), function(response) {
print("Google responded with " + response.body);
queue.shutdown();
var setTimeout = require("browser/timeout").setTimeout;
var deferred = require("promise").defer();
var queue = require("event-queue");
deferred.promise.then(function(){
print("done");
});
setTimeout(function(){
deferred.resolve(10);
queue.shutdown();
}, 500);
/**
* Takes an array of promises and returns a promise that that is fulfilled once all
* the promises in the array are fulfilled
* @param group The array of promises
* @return the promise that is fulfilled when all the array is fulfilled
*/exports.group = function(group){
var deferred = defer(); // this function is assigned process.Promise
if(!(group instanceof Array)){
group = Array.prototype.slice.call(arguments);
}
var posix = require("posix");
var print = require("sys").puts;
var j = 0;
for(var i = 0;i < 3; i++){
findFile(i);
}
function findFile(i){
posix.stat("file" + i, process.O_RDONLY, 0666) // these files don't exist
.addErrback(function(e){
j++; // only makes it to about 17
var posix = require("posix");
var print = require("sys").puts;
var j = 0;
for(var i = 0;i < 100; i++){
posix.stat("file" + i, process.O_RDONLY, 0666) // these files don't exist
.addErrback(function(e){
j++; // only makes it to about 17
print(j);
})
.addCallback(function(){
require.paths.push("../folder2");
require("b");
exports.name = "test";