ES6 中的空承诺 return 使用导入的 class

Empty promise return in ES6 using an imported class

我有一个问题,为什么我的回复被返回并打印为未定义。

这是我的方法 main.js:

find() {
        return this.serviceFactory
            .createRequest()
            .withId(333)
            .sendAsGet().then(response => {
                console.log(response);
            });
    }

上面的 console.log(response) 是我得到未定义响应的地方;

这是导入的 class,用作 this.serviceFactory

export class ServiceFactory {
    constructor(http) {
        this.http = http;
        this.http.baseUrl = serviceUrlParts.baseUrl;
        this.endpoint = "";
        this.id = "";
    }

    createRequest() {
        return this;
    }

    withId(id) {
        this.id = id;
        return this;
    }

    setEndPoint(endpoint){
        this.endpoint = serviceUrlParts.baseUrl + endpoint;
    }

    sendAsGet() {
        return this.http.get(`${this.endpoint}/${this.id}`)
            .then(response => {
                this.parseJSONContent(response);
            }).catch(error => {
                console.log(error);
            });
    }

    parseJSONContent(response) {
        console.dir(JSON.parse(response.content));
        return JSON.parse(response.content);
    }
}

parseJSONContent 方法中的 console.dir(JSON.parse(response.content)) 打印出从 API 返回的正确对象。某处返回到呼叫它正在丢失。非常感谢能深入了解发生了什么错误!

这里你没有return值:

.then(response => {
    this.parseJSONContent(response);
})

改为:

.then(response => {
    return this.parseJSONContent(response);
})

.then(response => this.parseJSONContent(response))