function JSONToObject(jsonStr) {
return JSON.parse(jsonStr);
}
var obj = JSONToObject('{');
setTimeout(function A() {
setTimeout(function B() {
setTimeout(function C() {
throw new Error('Something terrible has happened!');
},0);
},0);
},0);
Error: Something terrible has happened!
at Timer.C (/AsyncJS/nestedErrors.js:4:13)
try {
setTimeout(function() {
throw new Error('Catch me if you can!');
},0);
} catch (e) {
console.error(e);
}
var fs = require('fs');
fs.readFile('fhgwgdz.txt',function(err,data) {
if (err) {
return console.error(err);
};
console.log(data.toString('utf8'));
});
$.get('/data',{
success: successHandler,failure: failureHandler
});
window.onerror = function(err) {
return true;
};
process.on('uncaughtException',function(err) {
console.error(err);
});
var myDomain = require('domain').create();
myDomain.run(function() {
setTimeout(function() {
throw new Error('Listen to me!')
},50);
});
myDomain.on('error',function(err) {
console.log('Error ignored!');
});
Error ignored!
asyncOpA(a,b,(err,result) => {
if (err) {
handleErrorA(err);
}
asyncOpB(c,result,result) => {
if (err) {
handleErrorB(err);
}
asyncOpB(d,result) => {
if (err) {
handlerErrorC(err);
}
finalOp(result);
});
});
});
async.waterfall([
(cb) => {
asyncOpA(a,result) => {
cb(err,c,result);
});
},(c,lastResult,cb) => {
asyncOpB(c,d,result);
})
},(d,cb) => {
asyncOpC(d,result);
});
}
],(err,finalResult) => {
if (err) {
handlerError(err);
}
finalOp(finalResult);
});
let async = {
waterfall: (methods,finalCb = _emptyFunction) => {
if (!_isArray(methods)) {
return finalCb(new Error('First argument to waterfall must be an array of functions'));
}
if (!methods.length) {
return finalCb();
}
function wrap(n) {
if (n === methods.length) {
return finalCb;
}
return function (err,...args) {
if (err) {
return finalCb(err);
}
methods[n](...args,wrap(n + 1));
}
}
wrap(0)(false);
}
};
function toPromiseStyle(fn) {
return (...args) => {
return new Promise((resolve,reject) => {
fn(...args,result) => {
if (err) reject(err);
resolve(result);
})
});
};
}
let [opA,opB,opC] = [asyncOpA,asyncOpB,asyncOpC].map((fn) => toPromiseStyle(fn));
opA(a,b)
.then((res) => {
return opB(c,res);
})
.then((res) => {
return opC(d,res);
})
.then((res) => {
return finalOp(res);
})
.catch((err) => {
handleError(err);
});
function* getResult() {
let res,a,d;
try {
res = yield opA(a,b);
res = yield opB(c,res);
res = yield opC(d);
return res;
} catch (err) {
return handleError(err);
}
}
function spawn(genF,...args) {
return new Promise((resolve,reject) => {
let gen = genF(...args);
function next(fn) {
try {
let r = fn();
if (r.done) {
resolve(r.value);
}
Promise.resolve(r.value)
.then((v) => {
next(() => {
return gen.next(v);
});
}).catch((err) => {
next(() => {
return gen.throw(err);
})
});
} catch (err) {
reject(err);
}
}
next(() => {
return gen.next(undefined);
});
});
}
spawn(getResult)
.then((res) => {
finalOp(res);
})
.catch((err) => {
handleFinalOpError(err);
});
async function getResult() {
let res,d;
try {
res = await opA(a,b);
res = await opB(c,res);
res = await opC(d);
return res;
} catch (err) {
return handleError(err);
}
}
getResult();