(by @andrestaltz)
If you prefer to watch video tutorials with live-coding, then check out this series I recorded with the same contents as in this article: Egghead.io - Introduction to Reactive Programming.
| var listToArr = function(lst) { | |
| var l = lst.length; | |
| var arr = new Array(l); // optimizable *list-to-array | |
| for (var i = 0; i < l; ++i) { | |
| arr[i] = lst[i]; | |
| } | |
| return arr; | |
| }; |
| var ctx = this; // this can be skipped if you don't care about the context | |
| var argsL = arguments.length; | |
| var args = new Array(argsL); // optimizable arguments-to-array | |
| for(var i = 0; i < argsL; ++i) { | |
| args[i] = arguments[i]; | |
| } |
| var removeCycles = function(o) { | |
| var seen = []; | |
| var s = JSON.stringify(o, function(k, v) { | |
| if (v !== null && typeof v === 'object') { | |
| if (seen.indexOf(v) !== -1) { | |
| //return; | |
| v = '[removed]'; | |
| } | |
| else { |
| var os = require('os'); | |
| var getMyIPs = function() { | |
| var ips = {}; | |
| var ifaces = os.networkInterfaces(); | |
| var onDetails = function(details) { | |
| if (details.family === 'IPv4') { | |
| ips[ dev ] = details.address; | |
| } |
(by @andrestaltz)
If you prefer to watch video tutorials with live-coding, then check out this series I recorded with the same contents as in this article: Egghead.io - Introduction to Reactive Programming.
| #!/usr/bin/env phantomjs | |
| var system = require('system'); | |
| var text = encodeURIComponent(system.stdin.read()); | |
| var url = "http://translate.google.com/#auto/en/" + text; | |
| var page = require('webpage').create(); | |
| page.settings.userAgent = 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:13.0) Gecko/20100101 Firefox/13.0'; | |
| page.onConsoleMessage = function (msg) { |
| // Here’s a 100% deterministic alternative to `Math.random`. Google’s V8 and | |
| // Octane benchmark suites use this to ensure predictable results. | |
| Math.random = (function() { | |
| var seed = 0x2F6E2B1; | |
| return function() { | |
| // Robert Jenkins’ 32 bit integer hash function | |
| seed = ((seed + 0x7ED55D16) + (seed << 12)) & 0xFFFFFFFF; | |
| seed = ((seed ^ 0xC761C23C) ^ (seed >>> 19)) & 0xFFFFFFFF; | |
| seed = ((seed + 0x165667B1) + (seed << 5)) & 0xFFFFFFFF; |
This article has been given a more permanent home on my blog. Also, since it was first written, the development of the Promises/A+ specification has made the original emphasis on Promises/A seem somewhat outdated.
Promises are a software abstraction that makes working with asynchronous operations much more pleasant. In the most basic definition, your code will move from continuation-passing style:
getTweetsFor("domenic", function (err, results) {
// the rest of your code goes here.