Skip to content

Instantly share code, notes, and snippets.

@commuterjoy
Last active August 29, 2015 14:11
Show Gist options
  • Save commuterjoy/975c2d44899c56cab297 to your computer and use it in GitHub Desktop.
Save commuterjoy/975c2d44899c56cab297 to your computer and use it in GitHub Desktop.
async error inside a promise
'use strict';
require('es6-promise').polyfill();
var domain = require('domain');
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('ok')
})
// kill the process
app.get('/error', function (req, res) {
var a = new Promise(function(resolve, reject) {
setTimeout(function() {
throw new Error('Asynchronous error from timeout');
}, 500);
});
a
.then(function () {
res.send('ok from error')
})
.catch(function () {
res.send('not ok from error')
})
})
// doesn't kill the process
app.get('/error-w-domain', function (req, res) {
var d = domain.create();
d.on('error', function(err) {
console.log(err.stack);
res.send('error')
});
d.run(function() {
var a = new Promise(function(resolve, reject) {
setTimeout(function() {
throw new Error('Asynchronous error from timeout');
}, 500);
});
a
.then(function () {
res.send('ok from error')
})
.catch(function () {
res.send('not ok from error')
})
});
})
var port = Number(process.env.PORT || 5003);
var server = app.listen(port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment