Skip to content

Instantly share code, notes, and snippets.

@bloodyowl
Created August 1, 2014 16:20
Show Gist options
  • Save bloodyowl/cb7dfeab67217e4ee41a to your computer and use it in GitHub Desktop.
Save bloodyowl/cb7dfeab67217e4ee41a to your computer and use it in GitHub Desktop.
requirebin sketch
var promise = require("bloody-promise")
var log = function(string){
document.body.appendChild(document.createTextNode(string))
document.body.appendChild(document.createElement("br"))
}
var mapAsync = function(array, fn){
var index = -1
var length = array.length
var promises = []
while(++index < length) {
void function(item, index, array){
var promiseItem = promise.create(function(resolve, reject){
fn.call(promiseItem, item, index, array)
})
promises.push(promiseItem)
}(array[index], index, array)
}
return promise.all(promises)
}
mapAsync(
[1,2,3,4],
function(item){
var self = this
setTimeout(function(){
log(item)
self.resolve(item * 2)
}, item * 500)
})
.then(function(mapped){
log(mapped)
})
require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){var process=module.exports={};process.nextTick=function(){var canSetImmediate=typeof window!=="undefined"&&window.setImmediate;var canPost=typeof window!=="undefined"&&window.postMessage&&window.addEventListener;if(canSetImmediate){return function(f){return window.setImmediate(f)}}if(canPost){var queue=[];window.addEventListener("message",function(ev){var source=ev.source;if((source===window||source===null)&&ev.data==="process-tick"){ev.stopPropagation();if(queue.length>0){var fn=queue.shift();fn()}}},true);return function nextTick(fn){queue.push(fn);window.postMessage("process-tick","*")}}return function nextTick(fn){setTimeout(fn,0)}}();process.title="browser";process.browser=true;process.env={};process.argv=[];function noop(){}process.on=noop;process.once=noop;process.off=noop;process.emit=noop;process.binding=function(name){throw new Error("process.binding is not supported")};process.cwd=function(){return"/"};process.chdir=function(dir){throw new Error("process.chdir is not supported")}},{}],FQiZT6:[function(require,module,exports){var events=require("bloody-events");var asap=require("asap");var map={1:"resolve",2:"reject",3:"reject"};var identity=function(value){return value};var createCallback=function(current,then,fn,cb){return function(){asap(function(){var value;try{value=fn(current.value)}catch(e){then.reject(e);then.emit("error",e);return}if(value!=null&&typeof value.then=="function"){value.then(then.accessor("resolve"),then.accessor("reject"));return}if(fn===identity){cb(value)}else{then.accessor("resolve")(value)}})}};var promise=events.extend({PENDING:0,RESOLVED:1,REJECTED:2,ERROR:3,constructor:function(fn){var current=this;events.constructor.call(this);this.state=this.PENDING;this.on("resolve",function(value){current.state=current.RESOLVED;current.value=value;current.emit("done",value)});this.on("reject",function(value){current.state=current.REJECTED;current.value=value;current.emit("done",value)});this.on("error",function(value){current.state=current.REJECTED|current.ERROR;current.value=value;current.emit("done",value)});if(typeof fn!="function"){return}asap(function(){try{fn(current.accessor("resolve"),current.accessor("reject"))}catch(e){}})},destructor:function(){events.destructor.call(this)},resolve:function(value){var current=this;if(this.state!=this.PENDING){return}current.value=value;current.emit("resolve",value);current.emit("done",value)},reject:function(reason){var current=this;if(this.state!=this.PENDING){return}current.value=reason;current.emit("reject",reason);current.emit("done",reason)},then:function(resolveCallback,rejectCallback){var current=this;var then=promise.create();var state=map[this.state];if(typeof resolveCallback!="function"){resolveCallback=identity}if(typeof rejectCallback!="function"){rejectCallback=identity}var onResolve=createCallback(current,then,resolveCallback,function(value){then.resolve(value)});var onReject=createCallback(current,then,rejectCallback,function(value){then.reject(value)});if(this.state){asap(function(){if(state=="resolve"){onResolve();return}if(state=="reject"){onReject()}});return then}current.on("resolve",onResolve);current.on("reject",onReject);return then},"catch":function(rejectCallback){return this.then(null,rejectCallback)},done:function(callback){return this.then(callback,callback)},all:function(promises){var index=-1;var length=promises.length;var values=Array(length);var done=Array(length);var then=promise.create();var check=function(){var index=-1;while(++index<length){if(done[index]!=true){return}}then.resolve(values)};var bind=function(index){var item=promises[index];item.then(function(value){values[index]=value;done[index]=true;check()},function(reason){then.reject(reason)})};while(++index<length){bind(index)}check();return then},race:function(promises){var index=-1;var length=promises.length;var then=promise.create();var item;while(++index<length){item=promises[index];item.then(function(value){then.resolve(value)},function(reason){then.reject(reason)})}return then}});module.exports=promise},{asap:4,"bloody-events":5}],"bloody-promise":[function(require,module,exports){module.exports=require("FQiZT6")},{}],4:[function(require,module,exports){(function(process){var head={task:void 0,next:null};var tail=head;var flushing=false;var requestFlush=void 0;var isNodeJS=false;function flush(){while(head.next){head=head.next;var task=head.task;head.task=void 0;var domain=head.domain;if(domain){head.domain=void 0;domain.enter()}try{task()}catch(e){if(isNodeJS){if(domain){domain.exit()}setTimeout(flush,0);if(domain){domain.enter()}throw e}else{setTimeout(function(){throw e},0)}}if(domain){domain.exit()}}flushing=false}if(typeof process!=="undefined"&&process.nextTick){isNodeJS=true;requestFlush=function(){process.nextTick(flush)}}else if(typeof setImmediate==="function"){if(typeof window!=="undefined"){requestFlush=setImmediate.bind(window,flush)}else{requestFlush=function(){setImmediate(flush)}}}else if(typeof MessageChannel!=="undefined"){var channel=new MessageChannel;channel.port1.onmessage=flush;requestFlush=function(){channel.port2.postMessage(0)}}else{requestFlush=function(){setTimeout(flush,0)}}function asap(task){tail=tail.next={task:task,domain:isNodeJS&&process.domain,next:null};if(!flushing){flushing=true;requestFlush()}}module.exports=asap}).call(this,require("/home/admin/browserify-cdn/node_modules/browserify/node_modules/insert-module-globals/node_modules/process/browser.js"))},{"/home/admin/browserify-cdn/node_modules/browserify/node_modules/insert-module-globals/node_modules/process/browser.js":1}],5:[function(require,module,exports){var klass=require("bloody-class");var store=require("./lib/store");var slice=[].slice;module.exports=klass.extend({constructor:function(){this._events=store.create()},destructor:function(){this._events.destroy()},on:function(type,listener){this._events.push(type,listener);return this},once:function(type,listener){this._events.push(type,listener,true);return this},off:function(){this._events.remove.apply(this._events,arguments);return this},emit:function(type){return this._events.loop(type,slice.call(arguments,1))}})},{"./lib/store":6,"bloody-class":7}],6:[function(require,module,exports){var klass=require("bloody-class");var handleEventTypes={object:1,"function":1};module.exports=klass.extend({constructor:function(){this.store={}},destructor:function(){this.store={}},getTypeStore:function(type){if(this.store[type]){return this.store[type]}return this.store[type]=[]},indexOf:function(type,listener){var index=-1;var store=this.getTypeStore(type);var length=store.length;var item;while(++index<length){item=store[index];if(listener.listener==item.listener){return index}}return-1},has:function(type,listener){return this.indexOf(type,listener)!=-1},push:function(type,listener,once){var normalised=this.normalise(listener,once);if(this.has(type,normalised)){return}this.getTypeStore(type).push(normalised)},remove:function(type,listener){var normalised;var index;if(arguments.length==0){this.destroy();return}if(arguments.length==1){this.getTypeStore(type).length=0;return}normalised=this.normalise(listener);index=this.indexOf(type,normalised);if(index==-1){return}this.getTypeStore(type).splice(index,1)},loop:function(type,args){var store=this.getTypeStore(type);var index=-1;var length=store.length;var toRemove=[];var listener;var ran=false;while(++index<length){listener=store[index];if(--listener.remaining<0){toRemove.push(index);continue}if(typeof listener.callback=="object"){if(typeof listener.callback[type]=="function"){listener.callback[type].apply(listener.thisValue,args);ran=true}}else{listener.callback.apply(listener.thisValue,args);ran=true}}while(index=toRemove.pop()){store.splice(index,1)}return ran},normalise:function(listener){var callback;var handleEvent;var representation;if(typeof listener=="function"){callback=listener}if(listener!=null){handleEvent=listener.handleEvent;if(handleEvent!=null&&handleEventTypes[typeof handleEvent]){callback=listener.handleEvent}}return{listener:listener,callback:callback,thisValue:callback==listener?null:listener,remaining:arguments[1]?1:Infinity}}})},{"bloody-class":7}],7:[function(require,module,exports){var each=require("bloody-collections/lib/each");var mixinDontEnum={constructor:1,destructor:1};var extend=function(object,source,isMixin){each(source,function(item,key){if(isMixin&&mixinDontEnum[key]){return}object[key]=item})};var hasMethod=require("./lib/hasMethod");var create=require("./lib/create");var K=function(){};module.exports={extend:function(object){var subKlass=create(this);extend(subKlass,object);each(subKlass.mixins,function(mixin){extend(this,mixin,true)},subKlass);return subKlass},mixins:[],create:function(){var instance=create(this);var args=arguments;each(this.mixins,function(mixin){if(hasMethod(mixin,"constructor")){mixin.constructor.apply(instance,args)}},this);instance._accessors={};if(hasMethod(instance,"constructor")){instance.constructor.apply(instance,arguments)}return instance},destroy:function(){var args=arguments;if(hasMethod(this,"destructor")){this.destructor.apply(this,arguments)}each(this.mixins,function(mixin){if(hasMethod(mixin,"destructor")){mixin.destructor.apply(this,args)}},this);this._accessors={}},accessor:function(methodName){var thisValue=this;if(this._accessors.hasOwnProperty(methodName)){return this._accessors[methodName]}return this._accessors[methodName]=function(){return thisValue[methodName].apply(thisValue,arguments)}},constructor:K,destructor:K}},{"./lib/create":8,"./lib/hasMethod":9,"bloody-collections/lib/each":12}],8:[function(require,module,exports){var toString=Object.prototype.toString;var isNativeRE=RegExp("^"+String(toString).replace(/[.*+?^${}()|[\]\\]/g,"\\$&").replace(/toString| for [^\]]+/g,".*?")+"$");if(Object.create&&isNativeRE.test(Object.create)){module.exports=Object.create}else{module.exports=function(object){function F(){}F.prototype=object;return new F}}},{}],9:[function(require,module,exports){module.exports=function(object,property){return typeof object[property]=="function"}},{}],10:[function(require,module,exports){module.exports=[function(fn,thisValue){return function(){return fn.call(thisValue)}},function(fn,thisValue){return function(a){return fn.call(thisValue,a)}},function(fn,thisValue){return function(a,b){return fn.call(thisValue,a,b)}},function(fn,thisValue){return function(a,b,c){return fn.call(thisValue,a,b,c)}},function(fn,thisValue){return function(a,b,c,d){return fn.call(thisValue,a,b,c,d)}},function(fn,thisValue){return function(){return fn.apply(thisValue,arguments)}}]},{}],11:[function(require,module,exports){var callbacks=require("./_callbacks");module.exports=function(fn,thisValue,length){if(thisValue===void 0){return fn}if(length in callbacks){return callbacks[length](fn,thisValue)}return callbacks[callbacks.length-1](fn,thisValue)}},{"./_callbacks":10}],12:[function(require,module,exports){var getKeys=require("./getKeys"),createCallback=require("./createCallback"),isArrayLike=require("./isArrayLike");module.exports=function(collection,fn,thisValue){var index=-1,length,keys,key,callback=createCallback(fn,thisValue,3);if(!collection)return;if(isArrayLike(collection)){length=collection.length;while(++index<length){if(callback(collection[index],index,collection)===false)break}return}keys=getKeys(collection);length=keys.length;while(++index<length){key=keys[index];if(callback(collection[key],key,collection)===false)break}}},{"./createCallback":11,"./getKeys":13,"./isArrayLike":14}],13:[function(require,module,exports){var objectPrototype=Object.prototype,enumBugProperties=["constructor","hasOwnProperty","isPrototypeOf","propertyIsEnumerable",,"toLocaleString","toString","valueOf"],hasEnumBug=!objectPrototype.propertyIsEnumerable.call({constructor:1},"constructor"),_hasOwnProperty=objectPrototype.hasOwnProperty,hasObjectKeys=typeof Object.keys=="function",objectKeys=Object.keys;module.exports=function(object){var index,keys,length,enumKey;if(object==null)return[];if(hasObjectKeys)return objectKeys(object);keys=[];for(index in object){if(_hasOwnProperty.call(object,index))keys.push(index)}if(hasEnumBug){index=-1;length=enumBugProperties.length;while(++index<length){enumKey=enumBugProperties[index];if(_hasOwnProperty.call(object,enumKey)){keys.push(enumKey)}}}return keys}},{}],14:[function(require,module,exports){var _hasOwnProperty={}.hasOwnProperty;module.exports=function(object){var length;return object&&parseInt(length=object.length,10)===length&&!length||_hasOwnProperty.call(object,length-1)}},{}]},{},[]);var promise=require("bloody-promise");var log=function(string){document.body.appendChild(document.createTextNode(string));document.body.appendChild(document.createElement("br"))};var mapAsync=function(array,fn){var index=-1;var length=array.length;var promises=[];while(++index<length){void function(item,index,array){var promiseItem=promise.create(function(resolve,reject){fn.call(promiseItem,item,index,array)});promises.push(promiseItem)}(array[index],index,array)}return promise.all(promises)};mapAsync([1,2,3,4],function(item){var self=this;setTimeout(function(){log(item);self.resolve(item*2)},item*500)}).then(function(mapped){log(mapped)});
{
"name": "requirebin-sketch",
"version": "1.0.0",
"dependencies": {
"bloody-promise": "1.0.2"
}
}
<style type='text/css'>html, body { margin: 0; padding: 0; border: 0; }
body, html { height: 100%; width: 100%; }</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment