Skip to content

Instantly share code, notes, and snippets.

View Vidmich's full-sized avatar

Dmytro Vidmych Vidmich

View GitHub Profile
!function() {
var a = 1,
b = 2,
c = 3;
console.log(a + b);
console.log(a*b*c);
}();
function delay(v, t) {
var d = jQuery.Deferred();
setTimeout(function() {
d.resolve(v);
}, t);
return d.promise();
}
!function() {
var a = delay(1, 1000),
b = delay(2, 2000),
c = delay(3, 3000);
console.log(a + b);
console.log(a*b*c);
}();
function async(f) {
var g = f(); // get generator
function h(v, e) {
var t = e ? g.throw(e) : g.send(v);
if (t.done)
return;
if (t.value.then) {
t.value.then(function(v) {
async(function*() {
var a = yield delay(1, 1000),
b = yield delay(2, 2000),
c = yield delay(3, 3000);
console.log(a + b);
console.log(a*b*c);
});
async(function*() {
var a = delay(1, 1000),
b = delay(2, 2000),
c = delay(3, 3000);
a = yield a;
b = yield b;
console.log(a + b);
c = yield c;
async(function*() {
try {
var a = delay(1, 1000),
b = delay(2, 2000),
c;
try {
c = delay(3, 3000);
} catch(e) {
// something happened with "c" calculation
try {
async(function*() {
var a = yield delay(1, 1000);
throw new Error('some error');
});
} catch(e) {
console.log(e);
}
function async(f) {
var d = jQuery.Deferred();
var g = f(); // get generator
function h(v, e) {
var t;
try {
t = e ? g.throw(e) : g.send(v);
} catch(e) {
async(function*() {
try {
yield async(function*() {
var a = yield delay(1, 1000);
throw new Error('some error');
});
} catch(e) {
console.log(e);
}
});