Skip to content

Instantly share code, notes, and snippets.

@sheerun
Created October 19, 2016 06:44
Show Gist options
  • Save sheerun/68e182127f9acb894b43efed5f592d6e to your computer and use it in GitHub Desktop.
Save sheerun/68e182127f9acb894b43efed5f592d6e to your computer and use it in GitHub Desktop.
'use strict';
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
Function.prototype.$asyncbind = function $asyncbind(self, catcher) {
'use strict';
if (!Function.prototype.$asyncbind) {
Object.defineProperty(Function.prototype, '$asyncbind', {
value: $asyncbind,
enumerable: false,
configurable: true,
writable: true
});
}
if (!$asyncbind.trampoline) {
$asyncbind.trampoline = function trampoline(t, x, s, e) {
return function b(q) {
while (q) {
if (q.then) return q.then(b, e);
try {
if (q.pop) {
if (q.length) return q.pop() ? x.call(t) : q;
q = s;
} else q = q.call(t);
} catch (r) {
return e(r);
}
}
};
};
}
if (!$asyncbind.LazyThenable) {
$asyncbind.LazyThenable = function () {
function isThenable(obj) {
return obj && obj instanceof Object && typeof obj.then === 'function';
}
function resolution(p, r, how) {
try {
var x = how ? how(r) : r;
if (p === x) return p.reject(new TypeError('Promise resolution loop'));
if (isThenable(x)) {
x.then(function (y) {
resolution(p, y);
}, function (e) {
p.reject(e);
});
} else {
p.resolve(x);
}
} catch (ex) {
p.reject(ex);
}
}
function Chained() {}
;
Chained.prototype = {
resolve: _unchained,
reject: _unchained,
then: thenChain
};
function _unchained(v) {}
function thenChain(res, rej) {
this.resolve = res;
this.reject = rej;
}
function then(res, rej) {
var chain = new Chained();
try {
this._resolver(function (value) {
return isThenable(value) ? value.then(res, rej) : resolution(chain, value, res);
}, function (ex) {
resolution(chain, ex, rej);
});
} catch (ex) {
resolution(chain, ex, rej);
}
return chain;
}
function Thenable(resolver) {
this._resolver = resolver;
this.then = then;
}
;
Thenable.resolve = function (v) {
return Thenable.isThenable(v) ? v : {
then: function then(resolve) {
return resolve(v);
}
};
};
Thenable.isThenable = isThenable;
return Thenable;
}();
$asyncbind.EagerThenable = $asyncbind.Thenable = ($asyncbind.EagerThenableFactory = function (tick) {
tick = tick || (typeof process === 'undefined' ? 'undefined' : _typeof(process)) === 'object' && process.nextTick || typeof setImmediate === 'function' && setImmediate || function (f) {
setTimeout(f, 0);
};
var soon = function () {
var fq = [],
fqStart = 0,
bufferSize = 1024;
function callQueue() {
while (fq.length - fqStart) {
fq[fqStart]();
fq[fqStart++] = undefined;
if (fqStart === bufferSize) {
fq.splice(0, bufferSize);
fqStart = 0;
}
}
}
return function (fn) {
fq.push(fn);
if (fq.length - fqStart === 1) tick(callQueue);
};
}();
function Zousan(func) {
if (func) {
var me = this;
func(function (arg) {
me.resolve(arg);
}, function (arg) {
me.reject(arg);
});
}
}
Zousan.prototype = {
resolve: function resolve(value) {
if (this.state !== undefined) return;
if (value === this) return this.reject(new TypeError('Attempt to resolve promise with self'));
var me = this;
if (value && (typeof value === 'function' || (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object')) {
try {
var first = 0;
var then = value.then;
if (typeof then === 'function') {
then.call(value, function (ra) {
if (!first++) {
me.resolve(ra);
}
}, function (rr) {
if (!first++) {
me.reject(rr);
}
});
return;
}
} catch (e) {
if (!first) this.reject(e);
return;
}
}
this.state = STATE_FULFILLED;
this.v = value;
if (me.c) soon(function () {
for (var n = 0, l = me.c.length; n < l; n++) {
STATE_FULFILLED(me.c[n], value);
}
});
},
reject: function reject(reason) {
if (this.state !== undefined) return;
this.state = STATE_REJECTED;
this.v = reason;
var clients = this.c;
if (clients) soon(function () {
for (var n = 0, l = clients.length; n < l; n++) {
STATE_REJECTED(clients[n], reason);
}
});
},
then: function then(onF, onR) {
var p = new Zousan();
var client = {
y: onF,
n: onR,
p: p
};
if (this.state === undefined) {
if (this.c) this.c.push(client);else this.c = [client];
} else {
var s = this.state,
a = this.v;
soon(function () {
s(client, a);
});
}
return p;
}
};
function STATE_FULFILLED(c, arg) {
if (typeof c.y === 'function') {
try {
var yret = c.y.call(undefined, arg);
c.p.resolve(yret);
} catch (err) {
c.p.reject(err);
}
} else c.p.resolve(arg);
}
function STATE_REJECTED(c, reason) {
if (typeof c.n === 'function') {
try {
var yret = c.n.call(undefined, reason);
c.p.resolve(yret);
} catch (err) {
c.p.reject(err);
}
} else c.p.reject(reason);
}
Zousan.resolve = function (val) {
if (val && val instanceof Zousan) return val;
var z = new Zousan();
z.resolve(val);
return z;
};
Zousan.reject = function (err) {
if (err && err instanceof Zousan) return err;
var z = new Zousan();
z.reject(err);
return z;
};
Zousan.version = '2.3.2-nodent';
return Zousan;
})();
}
var resolver = this;
switch (catcher) {
case true:
return new $asyncbind.Thenable(boundThen);
case 0:
return new $asyncbind.LazyThenable(boundThen);
case undefined:
boundThen.then = boundThen;
return boundThen;
default:
return function () {
try {
return resolver.apply(self, arguments);
} catch (ex) {
return catcher(ex);
}
};
}
function boundThen() {
return resolver.apply(self, arguments);
}
};
function something() {
return new Promise(function ($return, $error) {
var result;
return fetch('http://www.google.pl').then(function ($await_1) {
result = $await_1;
return $return('foobar');
}.$asyncbind(this, $error), $error);
}.$asyncbind(this));
}
async function something(): Promise<string> {
const result = await fetch('http://www.google.pl');
return 'foobar';
}
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments)).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t;
return { next: verb(0), "throw": verb(1), "return": verb(2) };
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [0, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
function something() {
return __awaiter(this, void 0, void 0, function () {
var result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, fetch('http://www.google.pl')];
case 1:
result = _a.sent();
return [2 /*return*/, 'foobar'];
}
});
});
}
//# sourceMappingURL=index.js.map
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment