0

the code itself runs; could there be an obvious fix to this that i'm somehow missing?

the debugger in Webstorm highlights the reject statements during debugging, but the code never actually rejects; the debugger then highlights the fulfill statement and finally returns to the then block, however, the then block is immediately skipped in the next step and the catch block then runs

i have another promise wrapped around the then-catch code, which i'm also returning. my plan was to catch the rejection from the helper function, and after my main function catches it, reject it again as the return value (promise)
but while the debugger highlights the catch statement after getting back from the helper function, it appears that all the code inside the block is skipped over (after my helper function finishes in its entirety and gets back to the main function)

my fulfill-reject code:

public static filter(courses: Course[], filterObj: FILTER, comparison: string) { return new Promise(function (fulfill, reject) { let filter: any = (<any>filterObj)[comparison]; let names = Object.keys(filter); if (names.length !== 1) { console.log("??"); reject({"code": 400, "body": {"error": "invalid comparison"}}); } let name = names.toString(); if (!CHelpers.isDataset(name)) { reject({"code": 400, "body": {"error": "invalid comparison"}}); } let filterValue: number = filter[name]; if (typeof filterValue !== "number") { reject({"code": 400, "body": {"error": "invalid comparison"}}); } let key: string = CHelpers.getKey(name); console.log("successfully filtered " + comparison); fulfill(CHelpers.filterCourses(courses, key, filterValue, comparison)); }) } 

my then-catch code:

CHelpers.filter(courses, filterObj, comparison) .then(function(filteredCourses: any) { courses = filteredCourses; }) .catch(function(err: any) { return reject(err); }); 

my main function looks kind of like this:

doThing(input: any): Promise < {} > { return new Promise(function(fulfill, reject) { let courses: Course[]; let filterObj: FILTER = input.KEY; let comparisons: string[] = Object.keys(filterObj); let comparison: string = comparisons[0]; if (comparison === "test") { CHelpers.filter(courses, filterObj, comparison) .then(function(filteredCourses: any) { courses = filteredCourses; }) .catch(function(err: any) { return reject(err); }); } else { reject({ "code": 400, "body": { "error": "invalid body" } }); } let responseBody: {} = { render: "TABLE", result: courses }; fulfill({ "code": 200, "body": responseBody }); }); } 

2 Answers 2

2

reject() does not stop the execution of the code in that block. It's just a function call so after that function call, the rest of your .then() handler continues to run. Because promises are one-way state machines, the subsequent calls to reject() or fulfill() are ignored after you called reject(), but your other console.log() statements will execute.

Usually, when you return, you return right away from the function so that you don't execute any more code.

public static filter(courses: Course[], filterObj: FILTER, comparison: string) { return new Promise(function (fulfill, reject) { let filter: any = (<any>filterObj)[comparison]; let names = Object.keys(filter); if (names.length !== 1) { console.log("??"); return reject({"code": 400, "body": {"error": "invalid comparison"}}); } let name = names.toString(); if (!CHelpers.isDataset(name)) { return reject({"code": 400, "body": {"error": "invalid comparison"}}); } let filterValue: number = filter[name]; if (typeof filterValue !== "number") { return reject({"code": 400, "body": {"error": "invalid comparison"}}); } let key: string = CHelpers.getKey(name); console.log("successfully filtered " + comparison); fulfill(CHelpers.filterCourses(courses, key, filterValue, comparison)); }) } 

Note the addition of three return keywords on the same line as the reject() calls.

You can either code it as:

return reject(...); 

or as:

reject(...); return; 

Since reject() has no return value, these two forms are functionally equivalent.

Sign up to request clarification or add additional context in comments.

8 Comments

thank jfriend00, the helper function is rejecting properly, however, the code inside my catch block is not run (debugger only highlights the catch statement, but skips over the code inside); would that have to do with "subsequent rejects being ignored" or is it something else?
@icda - The reject(err); inside your .catch() looks wrong. If you meant to just continue the rejection, then you would either do throw err or return Promise.reject(err). There is no reject() defined inside a .catch(). When you say skips over the code in the .catch() are you saying that nothing in the .catch() runs or only some of the code in the .catch() runs?
@icda - I reread your question. If you were expecting to step into the reject and see it execute your .catch() block in the debugger, that will not happen. The actual .catch() is executed asynchronous after the current thread of JS finishes executing. You could set a breakpoint in the .catch() block and it should be hit.
@icda - Sorry, but I can't help with code I can't see. You do NOT want to wrap another promise around one you already have. That is a Promise anti-pattern. You should just return a promise from within a .then() handler. That's how you make the outer promise dependent upon the inner promise.
@icda - A helper function can certainly return a promise. In order to help you further, I'd need more to go on that "having trouble following your suggestion". I just suggested you add a few return statements.
|
1

You should try adding return statements for the fulfill and reject functions.

public static filter(courses: Course[], filterObj: FILTER, comparison: string) { return new Promise(function (fulfill, reject) { let filter: any = (<any>filterObj)[comparison]; let names = Object.keys(filter); if (names.length !== 1) { console.log("??"); return reject({"code": 400, "body": {"error": "invalid comparison"}}); } let name = names.toString(); if (!CHelpers.isDataset(name)) { return reject({"code": 400, "body": {"error": "invalid comparison"}}); } let filterValue: number = filter[name]; if (typeof filterValue !== "number") { return reject({"code": 400, "body": {"error": "invalid comparison"}}); } let key: string = CHelpers.getKey(name); console.log("successfully filtered " + comparison); return fulfill(CHelpers.filterCourses(courses, key, filterValue, comparison)); }) } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.