亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Angular 在 promise 中使用帶有泛型的 promise

Angular 在 promise 中使用帶有泛型的 promise

皈依舞 2021-12-02 20:02:39
我需要在我的應用程序中交錯承諾:  protected scroll<T>(path: string, pageSize: number, filter: string, data: T[]): Promise<T[]> {                let promise = new Promise<T[]>(function(resolve, reject) {                this.httpClient                    .get<T[]>(this.appConfigService.buildApiUrl(path), { params })                    .toPromise<T[]>()                    .then(result => {                        if (result) {                            resolve(data.concat(result));                        }                    })                    .catch(function(e) {                        reject(e);                    });            });            return promise;        }我的問題是我收到以下消息:“無類型函數調用可能不接受類型參數”我將如何解決這個問題?更新:我不應該從示例中刪除 if 條件:  if (!filter) {            const params = new HttpParams()                .set('searchText', '')                .set('skip', data.length.toString())                .set('take', pageSize.toString());            const promise = new Promise<T[]>((resolve, reject) => {                this.httpClient                    .get<T>(this.appConfigService.buildApiUrl(path), { params })                    .toPromise<any>()                    .then(result => {                        if (result) {                            resolve(data.concat(result));                        }                        resolve(data);                    })                    .catch(e => reject(e));            });            return promise;        }        // also returning a promise        return this.filter<T>(data, pageSize, filter, path);
查看完整描述

1 回答

?
猛跑小豬

TA貢獻1858條經驗 獲得超8個贊

那里有幾個問題。

  1. 錯誤消息是因為您使用的是<T[]>ongettoPromise,它們不是通用函數。只需在處理程序中應用類型T即可。resultthen

  2. 您正在陷入承諾創建反模式。您已經有一個承諾(來自this.httpClient),因此您不需要new Promise.

  3. 您正在為new Promise回調使用傳統函數,但隨后this在其中使用,就好像它仍在引用您的類實例一樣。如果你要保留new Promise,你會想要一個箭頭函數,所以它關閉了this。

相反(見***評論):

protected scroll<T>(path: string, pageSize: number, filter: string, data: T[]): Promise<T[]> {    

    // *** Return the result of calling `then` on the promise from `toPromise`

    return this.httpClient

        // *** Don't use <T[]> on `get` and `toPromise`

        .get(this.appConfigService.buildApiUrl(path), { params })

        .toPromise()

        .then((result: T) => { // *** <== Note the `T`

            // *** If it's really possible that `result` will be falsy and you don't want that

            // to be valid, you can do this:

            if (!result) {

                throw new Error("appropriate error here");

            }

            return data.concat(result);

        });

}

在操場上


更新:


我不應該從示例中刪除 if 條件:


沒關系,把上面的body放入ifblock即可:


protected scroll<T>(path: string, pageSize: number, filter: string, data: T[]): Promise<T[]> {    

    if (!filter) {

        const params = new HttpParams()

           .set('searchText', '')

           .set('skip', data.length.toString())

           .set('take', pageSize.toString());


        return this.httpClient

            // *** Don't use <T[]> on `get` and `toPromise`

            .get(this.appConfigService.buildApiUrl(path), { params })

            .toPromise()

            .then((result: T) => { // *** <== Note the `T`

                // *** If it's really possible that `result` will be falsy and you don't want that

                // to be valid, you can do this:

                if (!result) {

                    throw new Error("appropriate error here");

                }

                return data.concat(result);

            });

    }

    return this.filter<T>(data, pageSize, filter, path);

}


查看完整回答
反對 回復 2021-12-02
  • 1 回答
  • 0 關注
  • 328 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號