代码之家  ›  专栏  ›  技术社区  ›  methuselah

onSearch()返回的承诺,onSearchButtonClick()被忽略

  •  0
  • methuselah  · 技术社区  · 6 年前

    我一直收到以下警告: Promise returned from onSearch() is ignored 和 Promise returned from onSearchButtonClick() is ignored

      constructor(
        private unsplashService: UnsplashService
      ) {}
    
      async onSearch(event: KeyboardEvent) {
        if (event.key === "Enter") {
          return this.searchImages();
        }
      }
    
      async onSearchButtonClick() {
        return this.searchImages();
      }
    
      async searchImages() {
        if (!UtilService.isNullOrWhitespace(this.searchText)) {
          this.images = await this.unsplashService.searchImages(this.searchText);
        }
      }
    
      export class UnsplashService {
        getUnsplashApi(): Unsplash {
          return new Unsplash({
            accessKey: environment.unsplashAccessKey,
          })
       }
    
       async searchImages(query: String, pageIndex: number = 1, elementsPerPage: number = 30): Promise<any> {
         let response = await this.getUnsplashApi().search.photos(query, pageIndex, elementsPerPage);
         let json: any = await response.json();
         return await json.results;
      }
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Neji Sghair    6 年前

    从其中两个删除async。异步声明包装承诺。。如果你已经答应了,那你就不需要他们了

    推荐文章