Skip to content

Instantly share code, notes, and snippets.

@nilliams
Created Oct 21, 2016
Embed
What would you like to do?
requirebin sketch
var Promise = require('yaku');
function MyCoolErr() {}
function foo() {
return new Promise(function(resolve, reject) {
console.log("We're running...");
// Resolve and all is good ...
resolve();
});
}
foo()
.then(function() {
console.log("We're still running...");
// But then we go and throw ANY exception in here
// and the error is silently swallowed.
throw 'any runtime err, incl. typos, invalid code etc.'
console.log("We won't get here.");
})
.catch(function(err) {
if (err instanceof MyCoolErr) {
console.log('I know this error so will handle here');
return;
}
console.log('I did not recognise the error ...');
// And I re-threw it. I expect this to be an 'unhandled exception' and reach my devtools.
//
// ... And because Yaku has this right, I *do* see it in devtools/console, yay!
throw err;
});
setTimeout(function(){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);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.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}({yaku:[function(require,module,exports){(function(global){(function(){"use strict";var $undefined,$null=null,root=typeof window==="object"?window:global,isLongStackTrace=false,process=root.process,Arr=Array,Err=Error,$rejected=0,$resolved=1,$pending=2,$Symbol="Symbol",$iterator="iterator",$species="species",$speciesKey=$Symbol+"("+$species+")",$return="return",$unhandled="_uh",$promiseTrace="_pt",$settlerTrace="_st",$invalidThis="Invalid this",$invalidArgument="Invalid argument",$fromPrevious="\nFrom previous ",$promiseCircularChain="Chaining cycle detected for promise",$unhandledRejectionMsg="Uncaught (in promise)",$rejectionHandled="rejectionHandled",$unhandledRejection="unhandledRejection",$tryCatchFn,$tryCatchThis,$tryErr={e:$null},$noop=function(){},$cleanStackReg=/^.+\/node_modules\/yaku\/.+\n?/gm;var Yaku=module.exports=function Promise(executor){var self=this,err;if(!isObject(self)||self._s!==$undefined)throw genTypeError($invalidThis);self._s=$pending;if(isLongStackTrace)self[$promiseTrace]=genTraceInfo();if(executor!==$noop){if(!isFunction(executor))throw genTypeError($invalidArgument);err=genTryCatcher(executor)(genSettler(self,$resolved),genSettler(self,$rejected));if(err===$tryErr)settlePromise(self,$rejected,err.e)}};Yaku["default"]=Yaku;extendPrototype(Yaku,{then:function then(onFulfilled,onRejected){if(this._s===undefined)throw genTypeError();return addHandler(this,newCapablePromise(Yaku.speciesConstructor(this,Yaku)),onFulfilled,onRejected)},"catch":function(onRejected){return this.then($undefined,onRejected)},_pCount:0,_pre:$null,_Yaku:1});Yaku.resolve=function resolve(val){return isYaku(val)?val:settleWithX(newCapablePromise(this),val)};Yaku.reject=function reject(reason){return settlePromise(newCapablePromise(this),$rejected,reason)};Yaku.race=function race(iterable){var self=this,p=newCapablePromise(self),resolve=function(val){settlePromise(p,$resolved,val)},reject=function(val){settlePromise(p,$rejected,val)},ret=genTryCatcher(each)(iterable,function(v){self.resolve(v).then(resolve,reject)});if(ret===$tryErr)return self.reject(ret.e);return p};Yaku.all=function all(iterable){var self=this,p1=newCapablePromise(self),res=[],ret;function reject(reason){settlePromise(p1,$rejected,reason)}ret=genTryCatcher(each)(iterable,function(item,i){self.resolve(item).then(function(value){res[i]=value;if(!--ret)settlePromise(p1,$resolved,res)},reject)});if(ret===$tryErr)return self.reject(ret.e);if(!ret)settlePromise(p1,$resolved,[]);return p1};Yaku.Symbol=root[$Symbol]||{};genTryCatcher(function(){Object.defineProperty(Yaku,getSpecies(),{get:function(){return this}})})();Yaku.speciesConstructor=function(O,D){var C=O.constructor;return C?C[getSpecies()]||D:D};Yaku.unhandledRejection=function(reason,p){try{root.console.error($unhandledRejectionMsg,isLongStackTrace?p.longStack:genStackInfo(reason,p))}catch(e){}};Yaku.rejectionHandled=$noop;Yaku.enableLongStackTrace=function(){isLongStackTrace=true};Yaku.nextTick=process?process.nextTick:function(fn){setTimeout(fn)};Yaku._Yaku=1;function getSpecies(){return Yaku[$Symbol][$species]||$speciesKey}function extendPrototype(src,target){for(var k in target){src.prototype[k]=target[k]}return src}function isObject(obj){return obj&&typeof obj==="object"}function isFunction(obj){return typeof obj==="function"}function isInstanceOf(a,b){return a instanceof b}function isError(obj){return isInstanceOf(obj,Err)}function ensureType(obj,fn,msg){if(!fn(obj))throw genTypeError(msg)}function tryCatcher(){try{return $tryCatchFn.apply($tryCatchThis,arguments)}catch(e){$tryErr.e=e;return $tryErr}}function genTryCatcher(fn,self){$tryCatchFn=fn;$tryCatchThis=self;return tryCatcher}function genScheduler(initQueueSize,fn){var fnQueue=Arr(initQueueSize),fnQueueLen=0;function flush(){var i=0;while(i<fnQueueLen){fn(fnQueue[i],fnQueue[i+1]);fnQueue[i++]=$undefined;fnQueue[i++]=$undefined}fnQueueLen=0;if(fnQueue.length>initQueueSize)fnQueue.length=initQueueSize}return function(v,arg){fnQueue[fnQueueLen++]=v;fnQueue[fnQueueLen++]=arg;if(fnQueueLen===2)Yaku.nextTick(flush)}}function each(iterable,fn){var len,i=0,iter,item,ret;if(!iterable)throw genTypeError($invalidArgument);var gen=iterable[Yaku[$Symbol][$iterator]];if(isFunction(gen))iter=gen.call(iterable);else if(isFunction(iterable.next)){iter=iterable}else if(isInstanceOf(iterable,Arr)){len=iterable.length;while(i<len){fn(iterable[i],i++)}return i}else throw genTypeError($invalidArgument);while(!(item=iter.next()).done){ret=genTryCatcher(fn)(item.value,i++);if(ret===$tryErr){isFunction(iter[$return])&&iter[$return]();throw ret.e}}return i}function genTypeError(msg){return new TypeError(msg)}function genTraceInfo(noTitle){return(noTitle?"":$fromPrevious)+(new Err).stack}var scheduleHandler=genScheduler(999,function(p1,p2){var x,handler;handler=p1._s?p2._onFulfilled:p2._onRejected;if(handler===$undefined){settlePromise(p2,p1._s,p1._v);return}x=genTryCatcher(callHanler)(handler,p1._v);if(x===$tryErr){settlePromise(p2,$rejected,x.e);return}settleWithX(p2,x)});var scheduleUnhandledRejection=genScheduler(9,function(p){if(!hashOnRejected(p)){p[$unhandled]=1;emitEvent($unhandledRejection,p)}});function emitEvent(name,p){var browserEventName="on"+name.toLowerCase(),browserHandler=root[browserEventName];if(process&&process.listeners(name).length)name===$unhandledRejection?process.emit(name,p._v,p):process.emit(name,p);else if(browserHandler)browserHandler({reason:p._v,promise:p});else Yaku[name](p._v,p)}function isYaku(val){return val&&val._Yaku}function newCapablePromise(Constructor){if(isYaku(Constructor))return new Constructor($noop);var p,r,j;p=new Constructor(function(resolve,reject){if(p)throw genTypeError();r=resolve;j=reject});ensureType(r,isFunction);ensureType(j,isFunction);return p}function genSettler(self,state){return function(value){if(isLongStackTrace)self[$settlerTrace]=genTraceInfo(true);if(state===$resolved)settleWithX(self,value);else settlePromise(self,state,value)}}function addHandler(p1,p2,onFulfilled,onRejected){if(isFunction(onFulfilled))p2._onFulfilled=onFulfilled;if(isFunction(onRejected)){if(p1[$unhandled])emitEvent($rejectionHandled,p1);p2._onRejected=onRejected}if(isLongStackTrace)p2._pre=p1;p1[p1._pCount++]=p2;if(p1._s!==$pending)scheduleHandler(p1,p2);return p2}function hashOnRejected(node){if(node._umark)return true;else node._umark=true;var i=0,len=node._pCount,child;while(i<len){child=node[i++];if(child._onRejected||hashOnRejected(child))return true}}function genStackInfo(reason,p){var stackInfo=[];function push(trace){return stackInfo.push(trace.replace(/^\s+|\s+$/g,""))}if(isLongStackTrace){if(p[$settlerTrace])push(p[$settlerTrace]);(function iter(node){if(node&&$promiseTrace in node){iter(node._next);push(node[$promiseTrace]+"");iter(node._pre)}})(p)}return(reason&&reason.stack?reason.stack:reason)+("\n"+stackInfo.join("\n")).replace($cleanStackReg,"")}function callHanler(handler,value){return handler(value)}function settlePromise(p,state,value){var i=0,len=p._pCount;if(p._s===$pending){p._s=state;p._v=value;if(state===$rejected){if(isLongStackTrace&&isError(value)){value.longStack=genStackInfo(value,p)}scheduleUnhandledRejection(p)}while(i<len){scheduleHandler(p,p[i++])}}return p}function settleWithX(p,x){if(x===p&&x){settlePromise(p,$rejected,genTypeError($promiseCircularChain));return p}if(x!==$null&&(isFunction(x)||isObject(x))){var xthen=genTryCatcher(getThen)(x);if(xthen===$tryErr){settlePromise(p,$rejected,xthen.e);return p}if(isFunction(xthen)){if(isLongStackTrace&&isYaku(x))p._next=x;if(isYaku(x))settleXthen(p,x,xthen);else Yaku.nextTick(function(){settleXthen(p,x,xthen)})}else settlePromise(p,$resolved,x)}else settlePromise(p,$resolved,x);return p}function getThen(x){return x.then}function settleXthen(p,x,xthen){var err=genTryCatcher(xthen,x)(function(y){x&&(x=$null,settleWithX(p,y))},function(r){x&&(x=$null,settlePromise(p,$rejected,r))});if(err===$tryErr&&x){settlePromise(p,$rejected,err.e);x=$null}}})()}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{}]},{},[]);var Promise=require("yaku");function MyCoolErr(){}function foo(){return new Promise(function(resolve,reject){console.log("We're running...");resolve()})}foo().then(function(){console.log("We're still running...");throw"any runtime err, incl. typos, invalid code etc.";console.log("We won't get here.")}).catch(function(err){if(err instanceof MyCoolErr){console.log("I know this error so will handle here");return}console.log("I did not recognise the error ...");throw err})},0);
{
"name": "requirebin-sketch",
"version": "1.0.0",
"dependencies": {
"yaku": "0.16.7"
}
}
<!-- contents of this file will be placed inside the <body> -->
<div id="foo"></div>
<!-- contents of this file will be placed inside the <head> -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment