Last active
August 29, 2015 14:11
-
-
Save commuterjoy/975c2d44899c56cab297 to your computer and use it in GitHub Desktop.
async error inside a promise
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
'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