I was trying to understand JavaScript Promises by using various libraries (bluebird, when, Q) and other async approaches.
I read the spec, some blog posts, and looked through some code. I learned how to
#!/usr/bin/env python | |
""" | |
Very simple HTTP server in python (Updated for Python 3.7) | |
Usage: | |
./dummy-web-server.py -h | |
./dummy-web-server.py -l localhost -p 8000 | |
Send a GET request: |
/** | |
* Map over an Array using an asynchronous handler, maintaining a pool of concurrent handlers to be pending at all times until the Array is fully processed (as opposed to waiting until each async handler has finished before calling the next). | |
* | |
* @param {Array} list | |
* @param {Function} handler Called on each iteration (done:Function, element:Mixed, index:Number, list:Array) | |
* @param {Number} [options.maxConcurrent=5] The maximum number of handlers which can be running concurrently | |
* @param {Function} [options.done] Called once every handler has responded (responses:Array) | |
* @return {Array} The Array which is being populated with the values passed by handler to done() | |
*/ | |
exports.map = function (list, handler, options) { |
//Get a range of numbers between two numbers | |
//Usage: [1, 10].range returns [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
Object.defineProperty(Array.prototype, "range", { | |
get: function () { | |
var range = [this[0]], i; | |
for (var i = this[0], len = this[1]; i < len; range.push(++i)); | |
return range; | |
} | |
}); | |
// Golfed TypeScript: |
function promiseWhile(condition, body) { | |
var dfd = $.Deferred(); | |
function loop() { | |
if (!condition()) return dfd.resolve(); | |
body.apply(this, arguments) | |
.done(loop) | |
.fail(dfd.reject); | |
} | |
//call loop async |
var Thing = (function() { | |
this.public_property = "Public property"; | |
var private_property = "Private property"; | |
function Thing(data) { | |
this.datum1 = data.datum1; | |
this.datum3 = data.datum2; | |
} |
I was trying to understand JavaScript Promises by using various libraries (bluebird, when, Q) and other async approaches.
I read the spec, some blog posts, and looked through some code. I learned how to
function asyncParForEach(array, fn, callback) { | |
var completed = 0; | |
if (array.length === 0) { | |
callback(); // done immediately | |
} | |
array.forEach(function(data) { | |
fn(data, function() { | |
completed++; | |
if (completed === array.length) { |
/** | |
* Calculates the base bet from the current wager, multiplier, and streak. | |
* | |
* @param {number} wager - The current wager. | |
* @param {number} multi - The multiplier. | |
* @param {number} streak - The streak. | |
* @returns {number} - The calculated base bet. | |
*/ | |
const getBaseBetFromCurrent = (wager, multi, streak) => (wager / (multi ** streak)); |
// These hashes are for algorithmic use cases, such as bucketing in hashtables, where security isn't | |
// needed and 32 or 64 bits is enough (that is, rare collisions are acceptable). These are way simpler | |
// than sha1 (and all its deps) or similar, and with a short, clean (base 36 alphanumeric) result. | |
// A simple, *insecure* 32-bit hash that's short, fast, and has no dependencies. | |
// Output is always 7 characters. | |
// Loosely based on the Java version; see | |
// https://stackoverflow.com/questions/6122571/simple-non-secure-hash-function-for-javascript | |
const simpleHash = str => { | |
let hash = 0; |