This file contains hidden or 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
// new syntax | |
function addOne(value){ | |
return (when <: value) + 1; | |
} | |
// as effectively sugar for: | |
function addOne(value){ | |
return when(value, function(value){ | |
return value + 1; | |
}); | |
} |
This file contains hidden or 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
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; |
This file contains hidden or 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
require("./jsgi-node").start(function(request){ | |
var requestBody = request.input.read().decodeToString(); | |
return { | |
status:200, | |
headers:{}, | |
body:["echo: " + requestBody] | |
}; | |
}); |
This file contains hidden or 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
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(); |
This file contains hidden or 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
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); |
This file contains hidden or 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
/** | |
* 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); | |
} |
This file contains hidden or 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
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 |
This file contains hidden or 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
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(){ |
This file contains hidden or 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
require.paths.push("../folder2"); | |
require("b"); |
This file contains hidden or 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
exports.name = "test"; |