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
| function Spy(target, method) { | |
| var originalFunction = target[method] | |
| var result = { | |
| count: 0 | |
| } | |
| target[method] = function() { | |
| result.count++ | |
| return originalFunction.apply(target, 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
| function repeat(operation, num) { | |
| if (num <= 0) return | |
| operation() | |
| // release control every 10 or so | |
| // iterations. | |
| // 10 is arbitrary. | |
| if (num % 10 === 0) { | |
| setTimeout(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
| function repeat(operation, num) { | |
| if (num <= 0) { | |
| return num | |
| } | |
| else { | |
| operation() | |
| num-- | |
| return num | |
| } | |
| } |
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
| function loadUsers(userIds, load, done) { | |
| var users = [] | |
| var count = 0 | |
| userIds.forEach(function (id) { | |
| load(id, function (user) { | |
| users[userIds.indexOf(user.id)] = user | |
| count++ | |
| //can not use users.length here | |
| if (count == userIds.length) { |
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
| function getDependencies(mod, result) { | |
| result = result || [] | |
| var dependencies = mod.dependencies || [] | |
| Object.keys(dependencies).forEach(function(dep) { | |
| var key = dep + '@' + mod.dependencies[dep].version | |
| if (result.indexOf(key) === -1) result.push(key) | |
| getDependencies(mod.dependencies[dep], result) | |
| }) | |
| return result.sort() | |
| } |
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
| function curryN(fn, n) { | |
| function wrapper(args) { | |
| return function (oneArg) { | |
| var argsConcat = args.concat(oneArg) | |
| if (argsConcat.length == ( n || fn.length)) { | |
| return fn.apply(this, argsConcat) | |
| } | |
| return wrapper(argsConcat) | |
| } | |
| } |
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 defer = q.defer(); | |
| defer.promise.then(console.log) | |
| setTimeout(function() { | |
| defer.resolve("RESOLVED!") | |
| }, 300) |
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 defer = q.defer(); | |
| defer.promise.then(null, function(err) { | |
| console.log(err) | |
| }) | |
| setTimeout(function() { | |
| defer.reject("REJECTED!") | |
| }, 300) |
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 defer = q.defer(); | |
| defer.promise.then(console.log, console.log) | |
| defer.resolve("I FIRED") | |
| defer.reject("I DID NOT FIRE") |
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 defer = q.defer(); | |
| defer.promise.then(console.log) | |
| defer.resolve("SECOND") | |
| console.log("FIRST") | |
| //despite the promise being resolved synchronously, | |
| //the provided function is not executed until the next turn of the event loop. |