Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 承诺式的连续观测_Angular_Typescript_Rxjs_Angular2 Observables - Fatal编程技术网

Angular 承诺式的连续观测

Angular 承诺式的连续观测,angular,typescript,rxjs,angular2-observables,Angular,Typescript,Rxjs,Angular2 Observables,在探索可观察性时,我遇到了一个问题,即我只需要在另一个函数完成后执行函数。所有函数都是异步操作,使用@angular/Http中的Http服务 呼叫链应该是 检查播放列表是否存在。如果播放列表不存在,请创建它 获取推荐曲目 将推荐曲目添加到播放列表 通常情况下,我写这篇文章时会有承诺,并有一个看起来像 this.checkIfPlaylistExists() .then((exists) => { if(!exists) { return t

在探索可观察性时,我遇到了一个问题,即我只需要在另一个函数完成后执行函数。所有函数都是异步操作,使用
@angular/Http
中的
Http
服务

呼叫链应该是

  • 检查播放列表是否存在。如果播放列表不存在,请创建它
  • 获取
    推荐曲目
  • 将推荐曲目添加到播放列表
  • 通常情况下,我写这篇文章时会有承诺,并有一个看起来像

    this.checkIfPlaylistExists()
        .then((exists) => {
            if(!exists) {
                return this.createPlaylist();
            }
        })
        .then(() => this.getRecommendedTracks(seedTracks)) //resolves tracks
        .then(this.addRecommendedTracksToPlaylist)
        .then(doSomethingElse)
        .catch();
    
    我的困惑是我将如何使用可观测数据编写代码。到目前为止,我得到的不是100%有效,看起来是这样的

      createRecommendedTracksPlaylist(seedTracks: Track[]) {
        this.checkIfPlaylistExists()
          .map(exists => {
            if (!exists) {
              return this.createPlaylist();
            }
          })
          .map(() => this.getRecommendedTracks(seedTracks))
          .map((tracks) => this.addRecommendedTracksToPlaylist(tracks)) //<--- if I comment out this line, 'getRecommendedTracks' is executed and prints out response
          .mergeAll()
          .subscribe();
      }
    
    总而言之

    • 如何使用可观察对象编写这样的顺序执行顺序
    • 为什么函数
      getRecommendedTracks
      仅在我不让函数
      addRecommendedTracksToPlaylist
      在“链”中运行时执行
    • 我是否完全误解了
      Observable.map
      函数?我看过的所有示例都将其用于单个对象和数组,但我不确定是否正确使用了它

    只要将您的
    .map()
    更改为
    .flatMap()
    ,您就可以开始了:

    createRecommendedTracksPlaylist(seedTracks: Track[]) {
        this.checkIfPlaylistExists()
            .flatMap(exists => {
                if (!exists) {
                    return this.createPlaylist();
                }
                return Observable.of(1);
            })
            .flatMap(() => this.getRecommendedTracks(seedTracks))
            .flatMap((tracks) => this.addRecommendedTracksToPlaylist(tracks))
            .subscribe();
    }
    

    如果
    播放列表不存在,您将需要返回一个虚拟可观察对象,因为flatMap期望返回可观察对象
    Observable.empty()
    无法工作,因为它只是结束了流。

    .switchMap()是您的朋友可能的副本,谢谢您的回答!我尝试了这个,但是只执行了
    checkifplaylists
    ,其他什么都没有执行。我在问题中添加了这个函数,以防它是罪魁祸首@DanielB正在
    this.createPlaylist()
    返回一个
    可观察的
    调用?是的,它正在返回一个
    http.post
    调用,尽管在这种情况下播放列表始终存在@DanielB奇怪,
    flatMap
    相当于
    。那么
    在promiseI中我会继续查找,这可能与其他错误有关!我会让你知道的!
      private checkIfPlaylistExists() {
        const options = this.getAuthHeader();
        const playlistUrl = `https://api.spotify.com/v1/users/${this.userId}/playlists`;
        return this.http.get(playlistUrl, options).map((res) => {
          const playlists = res.json().items as Array<any>;
          return playlists.some((e, i, a) => {
            return e.name.indexOf(this.playlistTitle) > -1;
          })
        });
      }
    
    createRecommendedTracksPlaylist(seedTracks: Track[]) {
        this.checkIfPlaylistExists()
            .flatMap(exists => {
                if (!exists) {
                    return this.createPlaylist();
                }
                return Observable.of(1);
            })
            .flatMap(() => this.getRecommendedTracks(seedTracks))
            .flatMap((tracks) => this.addRecommendedTracksToPlaylist(tracks))
            .subscribe();
    }