Node unhandled promise rejections with require [duplicate]
This question already has an answer here:
Uncaught (in promise) Error
1 answer
Simplified code:
// File A
module.exports = new Promise((res, rej) => {
... do stuff
if (someErr) {
return rej(someErr);
}
return res(stuff)
}
// File B
const fileA = require('./fileA');
... express stuff
app.get('/getA', (req, res) => {
fileA
.then(data => res.send(data))
.catch(e => res.send(404, e));
});
As you can see the promise rejection gets caught when running a post request to a route in file B
. This way it is also possible to send a 404 response with the original error trough.
The problem is, there is an Unhandled Promise Rejection from file A
whenever the route is called. VERY annoying.
(node:5040) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:5040) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Yes, sure, i could catch the rejection in file A
, but i would rather prefer it propagate upstream and catch it later.
javascript node.js express promise
marked as duplicate by Bergi
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 14:35
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Uncaught (in promise) Error
1 answer
Simplified code:
// File A
module.exports = new Promise((res, rej) => {
... do stuff
if (someErr) {
return rej(someErr);
}
return res(stuff)
}
// File B
const fileA = require('./fileA');
... express stuff
app.get('/getA', (req, res) => {
fileA
.then(data => res.send(data))
.catch(e => res.send(404, e));
});
As you can see the promise rejection gets caught when running a post request to a route in file B
. This way it is also possible to send a 404 response with the original error trough.
The problem is, there is an Unhandled Promise Rejection from file A
whenever the route is called. VERY annoying.
(node:5040) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:5040) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Yes, sure, i could catch the rejection in file A
, but i would rather prefer it propagate upstream and catch it later.
javascript node.js express promise
marked as duplicate by Bergi
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 14:35
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Just dofileA.catch(err => console.debug("A will not be available (404) because "+err));
in the second file to handle the rejection immediately. You can still chain other handlers to the promise for answering requests. If you don't want a log message, writefileA.catch(err => { /* ignore */ });
(with a comment about where it will be handled later).
– Bergi
Nov 24 '18 at 14:37
add a comment |
This question already has an answer here:
Uncaught (in promise) Error
1 answer
Simplified code:
// File A
module.exports = new Promise((res, rej) => {
... do stuff
if (someErr) {
return rej(someErr);
}
return res(stuff)
}
// File B
const fileA = require('./fileA');
... express stuff
app.get('/getA', (req, res) => {
fileA
.then(data => res.send(data))
.catch(e => res.send(404, e));
});
As you can see the promise rejection gets caught when running a post request to a route in file B
. This way it is also possible to send a 404 response with the original error trough.
The problem is, there is an Unhandled Promise Rejection from file A
whenever the route is called. VERY annoying.
(node:5040) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:5040) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Yes, sure, i could catch the rejection in file A
, but i would rather prefer it propagate upstream and catch it later.
javascript node.js express promise
This question already has an answer here:
Uncaught (in promise) Error
1 answer
Simplified code:
// File A
module.exports = new Promise((res, rej) => {
... do stuff
if (someErr) {
return rej(someErr);
}
return res(stuff)
}
// File B
const fileA = require('./fileA');
... express stuff
app.get('/getA', (req, res) => {
fileA
.then(data => res.send(data))
.catch(e => res.send(404, e));
});
As you can see the promise rejection gets caught when running a post request to a route in file B
. This way it is also possible to send a 404 response with the original error trough.
The problem is, there is an Unhandled Promise Rejection from file A
whenever the route is called. VERY annoying.
(node:5040) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:5040) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Yes, sure, i could catch the rejection in file A
, but i would rather prefer it propagate upstream and catch it later.
This question already has an answer here:
Uncaught (in promise) Error
1 answer
javascript node.js express promise
javascript node.js express promise
asked Nov 24 '18 at 12:28
RasturRastur
67111
67111
marked as duplicate by Bergi
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 14:35
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Bergi
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 14:35
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Just dofileA.catch(err => console.debug("A will not be available (404) because "+err));
in the second file to handle the rejection immediately. You can still chain other handlers to the promise for answering requests. If you don't want a log message, writefileA.catch(err => { /* ignore */ });
(with a comment about where it will be handled later).
– Bergi
Nov 24 '18 at 14:37
add a comment |
Just dofileA.catch(err => console.debug("A will not be available (404) because "+err));
in the second file to handle the rejection immediately. You can still chain other handlers to the promise for answering requests. If you don't want a log message, writefileA.catch(err => { /* ignore */ });
(with a comment about where it will be handled later).
– Bergi
Nov 24 '18 at 14:37
Just do
fileA.catch(err => console.debug("A will not be available (404) because "+err));
in the second file to handle the rejection immediately. You can still chain other handlers to the promise for answering requests. If you don't want a log message, write fileA.catch(err => { /* ignore */ });
(with a comment about where it will be handled later).– Bergi
Nov 24 '18 at 14:37
Just do
fileA.catch(err => console.debug("A will not be available (404) because "+err));
in the second file to handle the rejection immediately. You can still chain other handlers to the promise for answering requests. If you don't want a log message, write fileA.catch(err => { /* ignore */ });
(with a comment about where it will be handled later).– Bergi
Nov 24 '18 at 14:37
add a comment |
1 Answer
1
active
oldest
votes
With the way you structured your code, the rejection is thrown before the .catch
is attached to the promise, so technically, it's was unhandled at the moment it was thrown.
I'm sure you have a reason why you would want to structure it like that (perhaps run "do stuff" only once?), so a possible workaround would be this:
// File A
let promise = null;
module.exports = () => {
if (! promise) {
promise = new Promise((res, rej) => {
... do stuff
if (someErr) {
return rej(someErr);
}
return res(stuff)
});
}
return promise;
};
// File B
app.get('/getA', (req, res) => {
fileA()
.then(data => res.send(data))
.catch(e => res.send(404, e));
});
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
With the way you structured your code, the rejection is thrown before the .catch
is attached to the promise, so technically, it's was unhandled at the moment it was thrown.
I'm sure you have a reason why you would want to structure it like that (perhaps run "do stuff" only once?), so a possible workaround would be this:
// File A
let promise = null;
module.exports = () => {
if (! promise) {
promise = new Promise((res, rej) => {
... do stuff
if (someErr) {
return rej(someErr);
}
return res(stuff)
});
}
return promise;
};
// File B
app.get('/getA', (req, res) => {
fileA()
.then(data => res.send(data))
.catch(e => res.send(404, e));
});
add a comment |
With the way you structured your code, the rejection is thrown before the .catch
is attached to the promise, so technically, it's was unhandled at the moment it was thrown.
I'm sure you have a reason why you would want to structure it like that (perhaps run "do stuff" only once?), so a possible workaround would be this:
// File A
let promise = null;
module.exports = () => {
if (! promise) {
promise = new Promise((res, rej) => {
... do stuff
if (someErr) {
return rej(someErr);
}
return res(stuff)
});
}
return promise;
};
// File B
app.get('/getA', (req, res) => {
fileA()
.then(data => res.send(data))
.catch(e => res.send(404, e));
});
add a comment |
With the way you structured your code, the rejection is thrown before the .catch
is attached to the promise, so technically, it's was unhandled at the moment it was thrown.
I'm sure you have a reason why you would want to structure it like that (perhaps run "do stuff" only once?), so a possible workaround would be this:
// File A
let promise = null;
module.exports = () => {
if (! promise) {
promise = new Promise((res, rej) => {
... do stuff
if (someErr) {
return rej(someErr);
}
return res(stuff)
});
}
return promise;
};
// File B
app.get('/getA', (req, res) => {
fileA()
.then(data => res.send(data))
.catch(e => res.send(404, e));
});
With the way you structured your code, the rejection is thrown before the .catch
is attached to the promise, so technically, it's was unhandled at the moment it was thrown.
I'm sure you have a reason why you would want to structure it like that (perhaps run "do stuff" only once?), so a possible workaround would be this:
// File A
let promise = null;
module.exports = () => {
if (! promise) {
promise = new Promise((res, rej) => {
... do stuff
if (someErr) {
return rej(someErr);
}
return res(stuff)
});
}
return promise;
};
// File B
app.get('/getA', (req, res) => {
fileA()
.then(data => res.send(data))
.catch(e => res.send(404, e));
});
answered Nov 24 '18 at 13:42
robertkleprobertklep
138k18235245
138k18235245
add a comment |
add a comment |
Just do
fileA.catch(err => console.debug("A will not be available (404) because "+err));
in the second file to handle the rejection immediately. You can still chain other handlers to the promise for answering requests. If you don't want a log message, writefileA.catch(err => { /* ignore */ });
(with a comment about where it will be handled later).– Bergi
Nov 24 '18 at 14:37