-
-
Save easyrider/f123100075a24fa1d72533c586e6eab5 to your computer and use it in GitHub Desktop.
Difference in error handling for async/await and callbacks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const request = require("request"); | |
const express = require("express"); | |
const util = require("util"); | |
const app = express(); | |
const requestPromise = util.promisify(request); | |
const timeoutPromise = util.promisify(setTimeout); | |
const getImgUrl = function(hostname, comicId, cb) { | |
request(`https://${hostname}/${comicId}/info.0.json`, function(err, resp, body) { | |
console.log("resp", resp); | |
if (err) { return cb(err); } | |
var temp = JSON.parse(body); | |
return cb(null, temp.img); | |
}); | |
} | |
const timeoutTest = function(cb) { | |
setTimeout(function() { | |
throw new Error("dead!"); | |
}, 10); | |
} | |
const timeoutTestPromise = async function() { | |
await timeoutPromise(10); | |
throw new Error("dead!"); | |
} | |
const getImgUrlPromise = async function(hostname, comicId) { | |
const resp = await requestPromise(`https://${hostname}/${comicId}/info.0.json`); | |
return JSON.parse(resp.body).img; | |
} | |
app.get("/", function(req, res, next) { | |
var urls = [ | |
"/callback_valid/", | |
"/callback_cb_error/", | |
"/callback_thrown_error/", | |
"/callback_timeout/", | |
"/promise_valid/", | |
"/promise_cb_error/", | |
"/promise_thrown_error/", | |
"/promise_timeout/" | |
]; | |
var html = urls.map(val => `<p><a href="${val}">${val}</a></p>`).join(""); | |
res.send(html); | |
}); | |
app.get("/callback_valid/", function(req, res, next) { | |
getImgUrl("xkcd.com", 2036, function(err, img) { | |
if (err) { return next(err); } | |
res.send(`<img src='${img}'/>`); | |
}); | |
}); | |
app.get("/callback_cb_error/", function(req, res, next) { | |
getImgUrl("ashfdakshdfajsdhfkjsa.com", "bogus", function(err, img) { | |
if (err) { return next(err); } | |
res.send(`<img src='${img}'/>`); | |
}); | |
}); | |
// process crash! | |
app.get("/callback_thrown_error/", function(req, res, next) { | |
getImgUrl("xkcd.com", "bogus", function(err, img) { | |
if (err) { return next(err); } | |
res.send(`<img src='${img}'/>`); | |
}); | |
}); | |
app.get("/callback_timeout/", function(req, res, next) { | |
// this try/catch obviously won't work... | |
try { | |
timeoutTest(); | |
} catch(e) { | |
return next(e); | |
} | |
}); | |
app.get("/promise_valid/", async function(req, res, next) { | |
try { | |
var img = await getImgUrlPromise("xkcd.com", 2036); | |
} catch(e) { | |
return next(e); | |
} | |
res.send(`<img src='${img}'/>`); | |
}); | |
app.get("/promise_cb_error/", async function(req, res, next) { | |
try { | |
var img = await getImgUrlPromise("xkcd.com", "bogus"); | |
} catch(e) { | |
return next(e); | |
} | |
res.send(`<img src='${img}'/>`); | |
}); | |
app.get("/promise_thrown_error/", async function(req, res, next) { | |
try { | |
var img = await getImgUrlPromise("xkcd.com", "bogus"); | |
} catch(e) { | |
return next(e); | |
} | |
res.send(`<img src='${img}'/>`); | |
}); | |
app.get("/promise_timeout/", async function(req, res, next) { | |
try { | |
await timeoutTestPromise(); | |
} catch(e) { | |
return next(e); | |
} | |
}); | |
app.listen(8080, function(err) { | |
if (err) { throw err; } | |
console.log("booted"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment