Unhandle Promise rejection inside try block
I'm having difficulties understanding why the code below throws an Unhandled Promise Rejection Warning :
router.post('/stampaClasse/:as(20[0-9][0-9]/[0-9][0-9])/:classe([1-8])',async function (req,res,next)
const sezioni = await Classi.getSezioniFromAnnoScolasticoAndClasse(req.params.as,req.params.classe);
let options =
semestre: req.body.semestre,
fontSize: req.body.fontSize,
textColor: '#515151',
gridColor: '#bec0be',
corDidattico:
titolo:'prof.',
nome:'Roberto',
cognome:'Dalema'
let newPDF = new pdfKit();
try
for(sezione of sezioni)
const idStudentiPromise = Classi.getStudentiFromAnnoScolasticoAndClasseAndSezione(req.params.as,req.params.classe,sezione)
const materiePromise = Classi.getMaterieSezione(req.params.as,req.params.classe,sezione)
const infoStudentiPromise = Promise.all( Studenti.getInfoStudentiById(await idStudentiResults) )
let classe =
annoScolastico: req.params.as,
classe : req.params.classe,
sezione: sezione,
materie: await materiePromise,
studenti: await infoStudentiPromise
for(studente of classe.studenti)
studente.pagelleMateriePromises = classe.materie.map(async m=>Pagelle.getPagellaFromStudente(classe.annoScolastico,classe.classe,classe.sezione,m,studente.id));
for(studente of classe.studenti)
studente.pagelleMaterie = await Promise.all(studente.pagelleMateriePromises)
addHeader(newPDF,studente,classe,options);
addPagelleSemestre(newPDF,studente,classe,options);
addFooter(newPDF,studente,classe,options);
newPDF.pipe(res);
newPDF.end();
catch(err)
next(err)
);
The error occurs multiple times, and it is caused by the fact that at line
const infoStudentiPromise = Promise.all( Studenti.getInfoStudentiById(await idStudentiResults) )
idStudentiResults alredy returns a Promise.all()
regardles of what is causing the error i'd like to know why it says the error is not beeing handled. Is the try catch not enaugh?
javascript node.js express promise
|
show 3 more comments
I'm having difficulties understanding why the code below throws an Unhandled Promise Rejection Warning :
router.post('/stampaClasse/:as(20[0-9][0-9]/[0-9][0-9])/:classe([1-8])',async function (req,res,next)
const sezioni = await Classi.getSezioniFromAnnoScolasticoAndClasse(req.params.as,req.params.classe);
let options =
semestre: req.body.semestre,
fontSize: req.body.fontSize,
textColor: '#515151',
gridColor: '#bec0be',
corDidattico:
titolo:'prof.',
nome:'Roberto',
cognome:'Dalema'
let newPDF = new pdfKit();
try
for(sezione of sezioni)
const idStudentiPromise = Classi.getStudentiFromAnnoScolasticoAndClasseAndSezione(req.params.as,req.params.classe,sezione)
const materiePromise = Classi.getMaterieSezione(req.params.as,req.params.classe,sezione)
const infoStudentiPromise = Promise.all( Studenti.getInfoStudentiById(await idStudentiResults) )
let classe =
annoScolastico: req.params.as,
classe : req.params.classe,
sezione: sezione,
materie: await materiePromise,
studenti: await infoStudentiPromise
for(studente of classe.studenti)
studente.pagelleMateriePromises = classe.materie.map(async m=>Pagelle.getPagellaFromStudente(classe.annoScolastico,classe.classe,classe.sezione,m,studente.id));
for(studente of classe.studenti)
studente.pagelleMaterie = await Promise.all(studente.pagelleMateriePromises)
addHeader(newPDF,studente,classe,options);
addPagelleSemestre(newPDF,studente,classe,options);
addFooter(newPDF,studente,classe,options);
newPDF.pipe(res);
newPDF.end();
catch(err)
next(err)
);
The error occurs multiple times, and it is caused by the fact that at line
const infoStudentiPromise = Promise.all( Studenti.getInfoStudentiById(await idStudentiResults) )
idStudentiResults alredy returns a Promise.all()
regardles of what is causing the error i'd like to know why it says the error is not beeing handled. Is the try catch not enaugh?
javascript node.js express promise
Promise.all takes an array of promises and resolves them in a then block. Check out this, it might be helpful: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– squeekyDave
Nov 12 '18 at 17:17
Hi! Please take the tour, have a look around, and read through the help center, in particular How do I ask a good question? I'd also check out Jon Skeet's question checklist and Writing the Perfect Question. Please update your question with a Minimal, Complete, and Verifiable example demonstrating the problem, ideally a runnable one using Stack Snippets (the[<>]
toolbar button; here's how to do one).
– T.J. Crowder
Nov 12 '18 at 17:18
1
You've saidModule.getInfo(ids)
returns a promise, not an array. If that's true (showing us would be much better), then why are you passing it intoPromise.all
? That would seem to serve no purpose...
– T.J. Crowder
Nov 12 '18 at 17:19
1
why it is saying the error is unhandled
because the error is unhanded. A normaltry / catch
is not able to catch a promise error, unless you usedasync / awat
, and didawait Promise.all()
– Keith
Nov 12 '18 at 17:23
Why are you wrapping a promise with a promise
– Kevin B
Nov 12 '18 at 17:28
|
show 3 more comments
I'm having difficulties understanding why the code below throws an Unhandled Promise Rejection Warning :
router.post('/stampaClasse/:as(20[0-9][0-9]/[0-9][0-9])/:classe([1-8])',async function (req,res,next)
const sezioni = await Classi.getSezioniFromAnnoScolasticoAndClasse(req.params.as,req.params.classe);
let options =
semestre: req.body.semestre,
fontSize: req.body.fontSize,
textColor: '#515151',
gridColor: '#bec0be',
corDidattico:
titolo:'prof.',
nome:'Roberto',
cognome:'Dalema'
let newPDF = new pdfKit();
try
for(sezione of sezioni)
const idStudentiPromise = Classi.getStudentiFromAnnoScolasticoAndClasseAndSezione(req.params.as,req.params.classe,sezione)
const materiePromise = Classi.getMaterieSezione(req.params.as,req.params.classe,sezione)
const infoStudentiPromise = Promise.all( Studenti.getInfoStudentiById(await idStudentiResults) )
let classe =
annoScolastico: req.params.as,
classe : req.params.classe,
sezione: sezione,
materie: await materiePromise,
studenti: await infoStudentiPromise
for(studente of classe.studenti)
studente.pagelleMateriePromises = classe.materie.map(async m=>Pagelle.getPagellaFromStudente(classe.annoScolastico,classe.classe,classe.sezione,m,studente.id));
for(studente of classe.studenti)
studente.pagelleMaterie = await Promise.all(studente.pagelleMateriePromises)
addHeader(newPDF,studente,classe,options);
addPagelleSemestre(newPDF,studente,classe,options);
addFooter(newPDF,studente,classe,options);
newPDF.pipe(res);
newPDF.end();
catch(err)
next(err)
);
The error occurs multiple times, and it is caused by the fact that at line
const infoStudentiPromise = Promise.all( Studenti.getInfoStudentiById(await idStudentiResults) )
idStudentiResults alredy returns a Promise.all()
regardles of what is causing the error i'd like to know why it says the error is not beeing handled. Is the try catch not enaugh?
javascript node.js express promise
I'm having difficulties understanding why the code below throws an Unhandled Promise Rejection Warning :
router.post('/stampaClasse/:as(20[0-9][0-9]/[0-9][0-9])/:classe([1-8])',async function (req,res,next)
const sezioni = await Classi.getSezioniFromAnnoScolasticoAndClasse(req.params.as,req.params.classe);
let options =
semestre: req.body.semestre,
fontSize: req.body.fontSize,
textColor: '#515151',
gridColor: '#bec0be',
corDidattico:
titolo:'prof.',
nome:'Roberto',
cognome:'Dalema'
let newPDF = new pdfKit();
try
for(sezione of sezioni)
const idStudentiPromise = Classi.getStudentiFromAnnoScolasticoAndClasseAndSezione(req.params.as,req.params.classe,sezione)
const materiePromise = Classi.getMaterieSezione(req.params.as,req.params.classe,sezione)
const infoStudentiPromise = Promise.all( Studenti.getInfoStudentiById(await idStudentiResults) )
let classe =
annoScolastico: req.params.as,
classe : req.params.classe,
sezione: sezione,
materie: await materiePromise,
studenti: await infoStudentiPromise
for(studente of classe.studenti)
studente.pagelleMateriePromises = classe.materie.map(async m=>Pagelle.getPagellaFromStudente(classe.annoScolastico,classe.classe,classe.sezione,m,studente.id));
for(studente of classe.studenti)
studente.pagelleMaterie = await Promise.all(studente.pagelleMateriePromises)
addHeader(newPDF,studente,classe,options);
addPagelleSemestre(newPDF,studente,classe,options);
addFooter(newPDF,studente,classe,options);
newPDF.pipe(res);
newPDF.end();
catch(err)
next(err)
);
The error occurs multiple times, and it is caused by the fact that at line
const infoStudentiPromise = Promise.all( Studenti.getInfoStudentiById(await idStudentiResults) )
idStudentiResults alredy returns a Promise.all()
regardles of what is causing the error i'd like to know why it says the error is not beeing handled. Is the try catch not enaugh?
javascript node.js express promise
javascript node.js express promise
edited Nov 13 '18 at 9:30
Filippo Bosco
asked Nov 12 '18 at 17:14
Filippo BoscoFilippo Bosco
134
134
Promise.all takes an array of promises and resolves them in a then block. Check out this, it might be helpful: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– squeekyDave
Nov 12 '18 at 17:17
Hi! Please take the tour, have a look around, and read through the help center, in particular How do I ask a good question? I'd also check out Jon Skeet's question checklist and Writing the Perfect Question. Please update your question with a Minimal, Complete, and Verifiable example demonstrating the problem, ideally a runnable one using Stack Snippets (the[<>]
toolbar button; here's how to do one).
– T.J. Crowder
Nov 12 '18 at 17:18
1
You've saidModule.getInfo(ids)
returns a promise, not an array. If that's true (showing us would be much better), then why are you passing it intoPromise.all
? That would seem to serve no purpose...
– T.J. Crowder
Nov 12 '18 at 17:19
1
why it is saying the error is unhandled
because the error is unhanded. A normaltry / catch
is not able to catch a promise error, unless you usedasync / awat
, and didawait Promise.all()
– Keith
Nov 12 '18 at 17:23
Why are you wrapping a promise with a promise
– Kevin B
Nov 12 '18 at 17:28
|
show 3 more comments
Promise.all takes an array of promises and resolves them in a then block. Check out this, it might be helpful: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– squeekyDave
Nov 12 '18 at 17:17
Hi! Please take the tour, have a look around, and read through the help center, in particular How do I ask a good question? I'd also check out Jon Skeet's question checklist and Writing the Perfect Question. Please update your question with a Minimal, Complete, and Verifiable example demonstrating the problem, ideally a runnable one using Stack Snippets (the[<>]
toolbar button; here's how to do one).
– T.J. Crowder
Nov 12 '18 at 17:18
1
You've saidModule.getInfo(ids)
returns a promise, not an array. If that's true (showing us would be much better), then why are you passing it intoPromise.all
? That would seem to serve no purpose...
– T.J. Crowder
Nov 12 '18 at 17:19
1
why it is saying the error is unhandled
because the error is unhanded. A normaltry / catch
is not able to catch a promise error, unless you usedasync / awat
, and didawait Promise.all()
– Keith
Nov 12 '18 at 17:23
Why are you wrapping a promise with a promise
– Kevin B
Nov 12 '18 at 17:28
Promise.all takes an array of promises and resolves them in a then block. Check out this, it might be helpful: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– squeekyDave
Nov 12 '18 at 17:17
Promise.all takes an array of promises and resolves them in a then block. Check out this, it might be helpful: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– squeekyDave
Nov 12 '18 at 17:17
Hi! Please take the tour, have a look around, and read through the help center, in particular How do I ask a good question? I'd also check out Jon Skeet's question checklist and Writing the Perfect Question. Please update your question with a Minimal, Complete, and Verifiable example demonstrating the problem, ideally a runnable one using Stack Snippets (the
[<>]
toolbar button; here's how to do one).– T.J. Crowder
Nov 12 '18 at 17:18
Hi! Please take the tour, have a look around, and read through the help center, in particular How do I ask a good question? I'd also check out Jon Skeet's question checklist and Writing the Perfect Question. Please update your question with a Minimal, Complete, and Verifiable example demonstrating the problem, ideally a runnable one using Stack Snippets (the
[<>]
toolbar button; here's how to do one).– T.J. Crowder
Nov 12 '18 at 17:18
1
1
You've said
Module.getInfo(ids)
returns a promise, not an array. If that's true (showing us would be much better), then why are you passing it into Promise.all
? That would seem to serve no purpose...– T.J. Crowder
Nov 12 '18 at 17:19
You've said
Module.getInfo(ids)
returns a promise, not an array. If that's true (showing us would be much better), then why are you passing it into Promise.all
? That would seem to serve no purpose...– T.J. Crowder
Nov 12 '18 at 17:19
1
1
why it is saying the error is unhandled
because the error is unhanded. A normal try / catch
is not able to catch a promise error, unless you used async / awat
, and did await Promise.all()
– Keith
Nov 12 '18 at 17:23
why it is saying the error is unhandled
because the error is unhanded. A normal try / catch
is not able to catch a promise error, unless you used async / awat
, and did await Promise.all()
– Keith
Nov 12 '18 at 17:23
Why are you wrapping a promise with a promise
– Kevin B
Nov 12 '18 at 17:28
Why are you wrapping a promise with a promise
– Kevin B
Nov 12 '18 at 17:28
|
show 3 more comments
1 Answer
1
active
oldest
votes
Promise.all
needs to receive an array of promises as param. Make sure that Module.getInfo(ids)
is returning that.
Maybe share the code forModule.getInfo
and tell us what exactly is that you're trying to achieve
– rubentd
Nov 12 '18 at 17:17
1
I know where the error lies, what i don't know is why it is saying the error is unhandled
– Filippo Bosco
Nov 12 '18 at 17:19
This line doesn't have a catch for the promises:const infoPromise = Promise.all( Module.getInfo(ids) );
– rubentd
Nov 12 '18 at 17:32
Do you mean i need a try-catch block for every line of code if that code execute some asyncronous operation?
– Filippo Bosco
Nov 12 '18 at 17:42
@FilippoBosco Not a try-catch block but the.catch
method for Promises
– rubentd
Nov 12 '18 at 18:01
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267033%2funhandle-promise-rejection-inside-try-block%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Promise.all
needs to receive an array of promises as param. Make sure that Module.getInfo(ids)
is returning that.
Maybe share the code forModule.getInfo
and tell us what exactly is that you're trying to achieve
– rubentd
Nov 12 '18 at 17:17
1
I know where the error lies, what i don't know is why it is saying the error is unhandled
– Filippo Bosco
Nov 12 '18 at 17:19
This line doesn't have a catch for the promises:const infoPromise = Promise.all( Module.getInfo(ids) );
– rubentd
Nov 12 '18 at 17:32
Do you mean i need a try-catch block for every line of code if that code execute some asyncronous operation?
– Filippo Bosco
Nov 12 '18 at 17:42
@FilippoBosco Not a try-catch block but the.catch
method for Promises
– rubentd
Nov 12 '18 at 18:01
add a comment |
Promise.all
needs to receive an array of promises as param. Make sure that Module.getInfo(ids)
is returning that.
Maybe share the code forModule.getInfo
and tell us what exactly is that you're trying to achieve
– rubentd
Nov 12 '18 at 17:17
1
I know where the error lies, what i don't know is why it is saying the error is unhandled
– Filippo Bosco
Nov 12 '18 at 17:19
This line doesn't have a catch for the promises:const infoPromise = Promise.all( Module.getInfo(ids) );
– rubentd
Nov 12 '18 at 17:32
Do you mean i need a try-catch block for every line of code if that code execute some asyncronous operation?
– Filippo Bosco
Nov 12 '18 at 17:42
@FilippoBosco Not a try-catch block but the.catch
method for Promises
– rubentd
Nov 12 '18 at 18:01
add a comment |
Promise.all
needs to receive an array of promises as param. Make sure that Module.getInfo(ids)
is returning that.
Promise.all
needs to receive an array of promises as param. Make sure that Module.getInfo(ids)
is returning that.
answered Nov 12 '18 at 17:16
rubentdrubentd
1,089821
1,089821
Maybe share the code forModule.getInfo
and tell us what exactly is that you're trying to achieve
– rubentd
Nov 12 '18 at 17:17
1
I know where the error lies, what i don't know is why it is saying the error is unhandled
– Filippo Bosco
Nov 12 '18 at 17:19
This line doesn't have a catch for the promises:const infoPromise = Promise.all( Module.getInfo(ids) );
– rubentd
Nov 12 '18 at 17:32
Do you mean i need a try-catch block for every line of code if that code execute some asyncronous operation?
– Filippo Bosco
Nov 12 '18 at 17:42
@FilippoBosco Not a try-catch block but the.catch
method for Promises
– rubentd
Nov 12 '18 at 18:01
add a comment |
Maybe share the code forModule.getInfo
and tell us what exactly is that you're trying to achieve
– rubentd
Nov 12 '18 at 17:17
1
I know where the error lies, what i don't know is why it is saying the error is unhandled
– Filippo Bosco
Nov 12 '18 at 17:19
This line doesn't have a catch for the promises:const infoPromise = Promise.all( Module.getInfo(ids) );
– rubentd
Nov 12 '18 at 17:32
Do you mean i need a try-catch block for every line of code if that code execute some asyncronous operation?
– Filippo Bosco
Nov 12 '18 at 17:42
@FilippoBosco Not a try-catch block but the.catch
method for Promises
– rubentd
Nov 12 '18 at 18:01
Maybe share the code for
Module.getInfo
and tell us what exactly is that you're trying to achieve– rubentd
Nov 12 '18 at 17:17
Maybe share the code for
Module.getInfo
and tell us what exactly is that you're trying to achieve– rubentd
Nov 12 '18 at 17:17
1
1
I know where the error lies, what i don't know is why it is saying the error is unhandled
– Filippo Bosco
Nov 12 '18 at 17:19
I know where the error lies, what i don't know is why it is saying the error is unhandled
– Filippo Bosco
Nov 12 '18 at 17:19
This line doesn't have a catch for the promises:
const infoPromise = Promise.all( Module.getInfo(ids) );
– rubentd
Nov 12 '18 at 17:32
This line doesn't have a catch for the promises:
const infoPromise = Promise.all( Module.getInfo(ids) );
– rubentd
Nov 12 '18 at 17:32
Do you mean i need a try-catch block for every line of code if that code execute some asyncronous operation?
– Filippo Bosco
Nov 12 '18 at 17:42
Do you mean i need a try-catch block for every line of code if that code execute some asyncronous operation?
– Filippo Bosco
Nov 12 '18 at 17:42
@FilippoBosco Not a try-catch block but the
.catch
method for Promises– rubentd
Nov 12 '18 at 18:01
@FilippoBosco Not a try-catch block but the
.catch
method for Promises– rubentd
Nov 12 '18 at 18:01
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267033%2funhandle-promise-rejection-inside-try-block%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Promise.all takes an array of promises and resolves them in a then block. Check out this, it might be helpful: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– squeekyDave
Nov 12 '18 at 17:17
Hi! Please take the tour, have a look around, and read through the help center, in particular How do I ask a good question? I'd also check out Jon Skeet's question checklist and Writing the Perfect Question. Please update your question with a Minimal, Complete, and Verifiable example demonstrating the problem, ideally a runnable one using Stack Snippets (the
[<>]
toolbar button; here's how to do one).– T.J. Crowder
Nov 12 '18 at 17:18
1
You've said
Module.getInfo(ids)
returns a promise, not an array. If that's true (showing us would be much better), then why are you passing it intoPromise.all
? That would seem to serve no purpose...– T.J. Crowder
Nov 12 '18 at 17:19
1
why it is saying the error is unhandled
because the error is unhanded. A normaltry / catch
is not able to catch a promise error, unless you usedasync / awat
, and didawait Promise.all()
– Keith
Nov 12 '18 at 17:23
Why are you wrapping a promise with a promise
– Kevin B
Nov 12 '18 at 17:28