Reputation: 36317
I'm working through a js/node workshop on async programming called promise-it-wont-hurt. I have the following exercise:
parsePromised
that creates a promise,performsJSON.parse
in a try
/catch
block, and fulfills or rejects the promise depending on whether an error is thrown. Note: your function should synchronously return the promise!my answer:
function parsePromised(json) {
return new Promise(function(resolve, reject){
resolve( JSON.parse(json) ),
reject(throwError)
});
}
parsePromised(process.argv[2]).then(console.log);
The stack trace is:
(node:3499) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse (<anonymous>)
at /home/optionhomes11/nodeprojects/promise-shop/test.js:184:21
at new Promise (<anonymous>)
at parsePromised (/home/optionhomes11/nodeprojects/promise-shop/test.js:183:10)
and the exercise result gives:
Your solution to Throw an error didn't pass. Try again!
Any idea how to get this working?
after changing to code in answer , the stack trace:
(node:5533) UnhandledPromiseRejectionWarning: SyntaxError:
Unexpected token u in JSON at position 0
at JSON.parse (<anonymous>)
at /home/optionhomes11/nodeprojects/promise-shop/test.js:186:34
at new Promise (<anonymous>)
at parsePromised (/home/optionhomes11/nodeprojects/promise-shop/test.js:184:10)
at Object.<anonymous> (/home/optionhomes11/nodeprojects/promise-shop/test.js:196:1)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Function.executeUserEntryPoint [as runMain]
(internal/modules/run_main.js:60:12)
(node:5533) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
Upvotes: 1
Views: 1750
Reputation: 665316
You were nearly there, you only missed the last step:
Build a sequence of steps like the ones shown above that catches any thrown errors and logs them to the console.
You were only logging the result if it was not an error. Change it to
parsePromised(process.argv[2]).catch(console.log);
// ^^^^^
Notice also that you don't need to explicitly call reject
- the Promise
constructor already handles exceptions thrown from the executor. So all you need to do is
function parsePromised(json) {
return new Promise(function(resolve, reject){
resolve( JSON.parse(json) )
});
}
If you wanted to catch the error yourself, you'd rather write
function parsePromised(json) {
try {
return Promise.resolve( JSON.parse(json) );
} catch(thrownError) {
return Promise.reject(thrownError);
}
}
Upvotes: 0
Reputation: 1503
According to the instruction you mentioned, I think you want to do it this way
function parsePromised(json) {
return new Promise(function(resolve, reject){
try {
const parsedJson = JSON.parse(json);
resolve(parsedJson);
} catch(error) {
// we reach in this block if we encounter error while parsing some invalid given json
reject(error);
//or reject(error.message);
}
});
}
Now you call it like this
parsePromised(process.argv[2])
.then(console.log)
.catch(function(err) {
console.log("some error occured while parsing the json");
console.log("error is:", err.message);
});
basically whatever you rejected in the promise goes to catch higher order function when you try to get the promise result.
Upvotes: 1