简体   繁体   中英

Stop Angular HTTP request without using unsubcribe

I am creating a music player but when the user skips a song it should stop the current download of a song and move ahead to the next one. I am using an async angular http function that resolves into a promise. I can not use subscribe() or unsubcribe() because that will just break the functionality

This is my code:

private async downloadSong(track: Track): Promise<string> {
    if(!isNullOrUndefined(this.request))
      console.log(this.request) // Returns: Response {_body: Blob, status: 200, ok: true, statusText: "OK", headers: Headers, …}

    this._downloadingSong = true;
    this.request = await this.http.get(track.source, {responseType: ResponseContentType.Blob}).toPromise();
    this._downloadingSong = false;
    return URL.createObjectURL(this.request.blob());
}

您应该使用takeUntil运算符停止可观察

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM