什么是 observable 等同于 `Promise.reject`

What's the observable equivalent to `Promise.reject`

我有这个代码

    return this.http.get(this.pushUrl)
        .toPromise()
        .then(response => response.json().data as PushResult[])
        .catch(this.handleError);

我想使用 observable 而不是 Promise

如何return调用方法的错误?

Promise.reject 等价于什么?

    doSomeGet() {
        console.info("sending get request");

        this.http.get(this.pushUrl)
            .forEach(function (response) { console.info(response.json()); })
            .catch(this.handleError);
    }

    private handleError(error: any) {
        console.error('An error occurred', error);
        // return Promise.reject(error.message || error);
    }
}

调用方法是:

getHeroes() {
    this.pushService
        .doSomeGet();
        // .then(pushResult => this.pushResult = pushResult)
        // .catch(error => this.error = error);
}
private handleError(error: any) {
    // previously 
    // return Observable.throw('Some error information');

    // now
    return throwError('Some error information');
}

另见

使用 RxJS 6 Observable.throw() 已更改为 throwError()

Observable.throw(new Error());

// becomes

throwError(new Error());

来源:RxJS v5.x to v6 Update Guide - Depracations