Skip to content

Instantly share code, notes, and snippets.

View benjamingr's full-sized avatar
๐Ÿ–Š๏ธ
Limited availability.

Benjamin Gruenbaum benjamingr

๐Ÿ–Š๏ธ
Limited availability.
View GitHub Profile
// returns a version of a given promise returning function fn that always uses the same cached value
function once(fn){
var called = false, cache = null;
return function cachedPromise(){
if(called) return cache;
cache = fn.apply(this,arguments);
called = true;
return cache;
};
}
[{"name":"11(None)","parent":"null"},{"name":"False","parent":"11(None)"},{"name":"2(None)","parent":"11(None)"},{"name":"True","parent":"2(None)"},{"name":"False","parent":"2(None)"},{"name":"False","parent":"11(None)"},{"name":"8(None)","parent":"null"},{"name":"False","parent":"8(None)"},{"name":"True","parent":"8(None)"},{"name":"6(None)","parent":"8(None)"},{"name":"True","parent":"6(None)"},{"name":"False","parent":"6(None)"},{"name":"False","parent":"6(None)"},{"name":"10(None)","parent":"null"},{"name":"14(None)","parent":"10(None)"},{"name":"2(None)","parent":"14(None)"},{"name":"True","parent":"2(None)"},{"name":"15(None)","parent":"2(None)"},{"name":"0(None)","parent":"15(None)"},{"name":"False","parent":"0(None)"},{"name":"True","parent":"0(None)"},{"name":"True","parent":"15(None)"},{"name":"True","parent":"14(None)"},{"name":"9(None)","parent":"14(None)"},{"name":"15(None)","parent":"9(None)"},{"name":"True","parent":"15(None)"},{"name":"1(None)","parent":"15(None)"},{"name":"False","parent":"1(
@benjamingr
benjamingr / gist:10536349
Created April 12, 2014 13:40
Bluebird asParallel
Promise.longStackTraces();
//end debug
/**
Hijack Promise.map to accept parallelism limit argument
**/
function queueConcurrent(work, degreeOfParallelism) {
// work is array of functions returning promises
"use strict";
<ul>
<li ng-repeat="Rating in Ratings | restrict: 'ratingHistory'" ng-click="showRating(Rating)" ng-class="Rating.rating" analytics="clicked-on-Rating-History" analytics-props="Rating.analyticsProps" >
<!--- ... --->
<div class="more-info" ng-show="Rating.show">
<!--- ... --->
<d3overview stock="Rating.stock" show-legend="false" show-duration="false" show-recommendations="true" />
</div>
</li>
</ul>
Entity._save = Promise.promisify(Item.prototype.save);
Entity.prototype._create = Promise.method(function(itemData) {
if (!this.Model) throw new Error('No Mongoose.Model defined, use setModel()');
var item = new this.Model(itemData);
return Entity._save.apply(item);
});

Tasks

For 24.11.2013

##Benji:

Tasks that relate mostly to fronend.

  • HTML/CSS for the page (won't look too good, not a designer)
@benjamingr
benjamingr / gist:7437587
Last active February 11, 2025 18:40
Chat room.

So, we figured creating our own chatroom could be fun.

It's something we can all work together on.

We don't plan on moving there but the project itself could be interesting for the whole room to collaborate work on.

What we need to discuss now:

  • What technologies we want to use (Node/Express seems natural for serverside. What about clientside?)
  • What features to we want? Do we just want to start with a clone of the SO chat?

Traditionally, JavaScript code contains some sort of asynchronous logic either on the client side or on the server side.

This makes the try/catch construct non-practical for many real use cases because it can not catch errors caused by callbacks and/or other asynchronous operations involved. This is why we have common idioms like the (err,result) of NodeJS callbacks or reject in promises implementations and so on.

NodeJS also introduces domains which let you catch all errors that originated from a piece of code in a certain way.

ES6 Generators recently implemented in NodeJS allow us to do concurrency using coroutines. This allows for very nice code that's very readable in certain cases. The async_functions proposal is also very interesting solving the problem without needing a "runner" for the generator.

However, one issue arises from using generators for such concurrency. The current way try/catch works with generators does not give us a way to differentiate things like syntax errors from logic err

ื“ื‘ืจื™ื ืฉืžืคืชื—ื™ JavaScript ืฆืจื™ื›ื™ื ืœื“ืขืช ืœื•ื•ื‘.

  1. ืื™ืš ืžื‘ืฆืขื™ื ื‘ืงืฉืช AJAX ืœืฉืจืช
  2. ืžื” ื–ื” AJAX
  3. ืื™ืš ืื ื™ ืžื—ื–ื™ืจ ืขืจืš ืžืงืจื™ืืช AJAX
  4. ืื™ืš ืžื‘ืฆืขื™ื ืื™ื˜ืจืฆื™ื” ืขืœ ืขื‘ืจื™ ืžืขืจืš
  5. ืื™ืš ืžื‘ืฆืขื™ื ืื™ื˜ืจืฆื™ื” ืขืœ ืื™ื‘ืจื™ ืื•ื‘ื™ื™ืงื˜
  6. ืื™ืš ืื ื™ ืžื•ืกื™ืฃ ืคืขื•ืœื” ืœืœื—ื™ืฆื” ืขืœ ื˜ืื’ HTML ืฉืจืฉืžืชื™
  7. ืื™ืš ืื ื™ ื™ื•ืฆืจ HTML ื—ื“ืฉ ืžJavaScript ื•ืžื•ืกื™ืฃ ืื•ืชื• ืœื“ืฃ
  8. ืื™ืš ืื ื™ ืฉื•ืžืจ ืžื™ื“ืข ื‘JavaScript ืฉื”ืžืฉืชืžืฉ ื™ื’ืฉ ืืœื™ื• ืคืขื ื”ื‘ืื” ืฉื”ื•ื ื—ื•ื–ืจ ืœื“ืฃ
"use strict";
function EventEmitter() {}
EventEmitter.prototype.on = EventEmitter.prototype.emit = function() {};