Skip to content

Instantly share code, notes, and snippets.

View offero's full-sized avatar

Chris offero

View GitHub Profile
@offero
offero / countWordsInText.js
Last active January 14, 2017 20:58 — forked from oscarmorrison/countWordsInText.js
count the words in a string, and order by popularity
function countWordsInText(text) {
let count = {};
let popularity = []; // maybe this should be an object due to JS array expansion logic
function updateWord(word, ct) {
if (popularity[ct-1]) {
popularity[ct-1].delete(word);
}
if (!popularity[ct]) {
popularity[ct] = new Set();
@offero
offero / promiseinacallback.js
Created November 24, 2016 17:41
Promise in a callback
function doThingThatAcceptsCallback(arg1, callback) {
doThingThatReturnsAPromise(arg1)
.then(function resolved(result) {
callback(null, result);
})
.catch(function rejected(error) {
callback(error);
});
}
@offero
offero / wrapcallbackinpromise.js
Created November 24, 2016 17:20
Wrap a callback and return a promise
function doThingThatReturnsPromise(arg1) {
return new Promise(function(resolve, reject) {
fnThatDoesTheThingButAcceptsACallback(arg1, function callback(err, result) {
if(err) {
// the callback was called with an error
return reject(err);
}
// the callback was called with a result
resolve(result);
});
@offero
offero / logDeferredTime.py
Created March 14, 2016 12:13
A wrapper around a log message to log the user time of a deferred.
def logDeferredTime(deferred, logfn, message, extra=None):
"""Returns a deferred that wraps the passed in deferred and logs how long
it took for the provided deferred to return (either callback or errback)
from the time of the calling of this function. This creates a proxy
Deferred that is passed all of the arguments that are passed to the
provided deferred.
deferred: The deferred that we wish to time.
logfn: A function that is called with (message, extra=extra). IE.
`logger.info`