代码本身运行;可能有一个明显的解决方案,我不知何故失踪?
Webstorm中的调试器在调试期间突出显示reject
语句,但代码从未实际拒绝;然后调试器突出显示fulfill
语句并最终返回到then
块,但是,在下一步中会立即跳过then
块,然后运行catch
块< / p>
我还有then-catch
代码附带的另一个承诺,我也会回复。我的计划是捕获辅助函数的拒绝,并在我的主函数捕获后,再次拒绝它作为返回值(承诺)
但是当调试器从辅助函数返回后突出显示catch
语句时,看起来块中的所有代码都被跳过(在我的辅助函数完全完成并返回到main函数之后)< / p>
我的履行拒绝代码:
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));
})
}
我当时的捕获代码:
CHelpers.filter(courses, filterObj, comparison)
.then(function(filteredCourses: any) {
courses = filteredCourses;
})
.catch(function(err: any) {
return reject(err);
});
我的主要功能看起来像这样:
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
});
});
}
答案 0 :(得分:2)
reject()
不会停止执行该块中的代码。它只是一个函数调用,所以在该函数调用之后,.then()
处理程序的其余部分继续运行。由于promises是单向状态机,因此在调用reject()
后会忽略对fulfill()
或reject()
的后续调用,但会执行其他console.log()
语句。
通常,当您返回时,您会立即返回该函数,以便您不再执行任何代码。
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));
})
}
请注意,在与return
调用相同的行中添加了三个reject()
个关键字。
您可以将其编码为:
return reject(...);
或作为:
reject(...);
return;
由于reject()
没有返回值,因此这两种形式在功能上是等价的。
答案 1 :(得分:1)
您应该尝试为履行和拒绝功能添加return语句。
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));
})
}