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
"use strict"; | |
var Q = require("../q"); | |
exports['test spread'] = function (ASSERT, done) { | |
Q.ref([1,2,3]) | |
.spread(function (a, b, c) { | |
ASSERT.equal(a, 1, 'spread 1'); | |
ASSERT.equal(b, 2, 'spread 2'); | |
ASSERT.equal(c, 3, 'spread 3'); |
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 q = require('q'); | |
var a = q.defer(); | |
var b = q.defer(); | |
q.when(a, function() { | |
console.log('calling b'); | |
return q.when(b, function() { | |
console.log('throwing'); | |
throw new Error('Hello World'); |
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
Copyright 2011 Kristopher Michael Kowal. All rights reserved. | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to | |
deal in the Software without restriction, including without limitation the | |
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
sell copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. |
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
defs = {}; | |
modules = {}; | |
function define(name, fn) { | |
defs[name] = fn; | |
} | |
function require(name) { | |
console.log("Loading " + name); | |
if (modules.hasOwnProperty(name)) return modules[name]; | |
if (defs.hasOwnProperty(name)) { | |
var fn = defs[name]; |
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 Q = require("q"); | |
var Queue = require("q/queue").Queue; | |
function throttle(max, wrapped) { | |
var queue = Queue(); | |
var pending = 0; | |
function loop() { | |
return Q.when(queue.get(), function (next) { |
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
#/usr/bin/env sh | |
check_and_exit () { | |
if [ ! "$?" = "0" ]; then | |
echo "$1" | |
exit 1 | |
fi | |
} | |
# Node.JS |
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.def({ | |
'increment': { | |
'requires': ['math'], | |
'factory': function() { | |
var exports = arguments[0].exports, | |
require = arguments[0].require, | |
module = arguments[0].module; | |
var add = require('math').add; | |
exports.increment = function(val) { | |
return add(val, 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
var HTTP = require("narwhal/q-http-client"); | |
var Q = require("narwhal/promise-util"); | |
// recovery | |
var httpReadRetryLoop = function (url, timeout, times) { | |
return Q.when(HTTP.read(url), function (content) { | |
return content; | |
}, function (error) { | |
if (times == 0) | |
return Q.reject(error); |
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 Q = require("promise"); // http://gist.github.com/312929 | |
function xhrcall() { | |
var deferred = Q.Deferred(); | |
return promise(function(P){ | |
var xhr = new XMLHttpRequest(); | |
xhr.open("GET","/"); | |
xhr.onreadystatechange = function(){ | |
if (xhr.readyState == 4) { | |
if (xhr.responseText.match(/success/)) |
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
// EXAMPLE 1 | |
// PSGI equivalent | |
var app = function(env) { | |
return function(respond) { | |
// do some event stuff | |
setTimeout(function() { | |
respond({ status : code, headers : headers, body : body }); | |
}, 1000); | |
} |