When I try to implement the code from the challenges in the ES6 course on my Notepad++ using Node.js I get errors (despite passing the tests with flying colors…)
I get this:
CD: D:\Nueva carpeta (2)\JavaScript Freecodecamp
Current directory: D:\Nueva carpeta (2)\JavaScript Freecodecamp
node freeCodeCampES6_27.js
Process started (PID=2104) >>>
node:internal/process/promises:288
triggerUncaughtException(err, true /* fromPromise */);
^
[UnhandledPromiseRejection: 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(). The promise rejected with the reason "Data not received".] {
code: 'ERR_UNHANDLED_REJECTION'
}
Node.js v18.7.0
<<< Process finished (PID=2104). (Exit code 1)
================ READY ================
when running this:
const makeServerRequest = new Promise((resolve, reject) => {
// responseFromServer represents a response from a server
let responseFromServer;
if(responseFromServer) {
// Change this line
resolve("We got the data");
} else {
// Change this line
reject("Data not received");
}
});
makeServerRequest.then(result => {
console.log(result);
});
Look carefully at the error message, read it, don’t just panic immediately when you see it.
It tells you exactly what you haven’t done here, in plain English.
Once you’ve looked at that, can you identify what is missing when you do makeServerRequest.then(......)? As I say, the error tells you exactly what that is
Just as a hint, which block is executed here? Is it resolve, or is it reject?
let responseFromServer;
if(responseFromServer) {
// Change this line
resolve("We got the data");
} else {
// Change this line
reject("Data not received");
}
Many thanks. The promise received a reject. It says to use the .catch() that is taught in the next module as this:
const makeServerRequest = new Promise((resolve, reject) => {
// responseFromServer is set to false to represent an unsuccessful response from a server
let responseFromServer = false;
if(responseFromServer) {
resolve("We got the data");
} else {
reject("Data not received");
}
});
makeServerRequest.then(result => {
console.log(result);
});
makeServerRequest.catch(error => {
console.log(error);
});
I ran this on my Notepad++ editor coupled with Node.js and the result was this:
CD: D:\Nueva carpeta (2)\JavaScript Freecodecamp
Current directory: D:\Nueva carpeta (2)\JavaScript Freecodecamp
node freeCodeCampES6_28.js
Process started (PID=14808) >>>
Data not received
node:internal/process/promises:288
triggerUncaughtException(err, true /* fromPromise */);
^
[UnhandledPromiseRejection: 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(). The promise rejected with the reason "Data not received".] {
code: 'ERR_UNHANDLED_REJECTION'
}
Node.js v18.7.0
<<< Process finished (PID=14808). (Exit code 1)
================ READY ================
Which is cool because I saw that the promise was rejected… Still, I am trying to figure out what’s wrong… I can’t seem to keep my promises…
You need to chain them together off the same Promise:
myPromise
.then(/* do something */)
.catch(/* do something */);
In theory you could do a lot of chainging:
myPromise
.then(/* do something */)
.then(/* do something */)
.then(/* do something */)
.then(/* do something */)
.catch(/* do something if an exception is thrown */)
.finally(/* do something at the end, no matter what */)
You can even have internal catches in different stages of the chain.