承诺全部-未处理的承诺被拒

时间:2018-09-04 03:28:36

标签: javascript node.js typescript promise es6-promise

做一些TTD并尝试抛出一个自定义异常,我称之为FailedToConnectError,但我一直收到NoAccountFoundError。

这是我的考试:

Server.SERVER_URL = 'http://www.test.com.fail';

server.getAccounts(['999999999']).catch(e=>{
    expect(e).toBeInstanceOf(FailedToConnectError);
});

具有下一个功能的主要功能是我们将帐户ID分组为100个,然后构建一组可运行的Promise。 (API一次最多只能允许您查看100个帐户):

public getAccounts(accountIDs: string[]): Promise<Account[]>{   
    return new Promise<Account[]>((resolve, reject) => { 
        let groupedAccountIds:string[] = this.groupAccountIdsByMaxRquestLimit(accountIDs);
        let promises: Promise<Account[]>[] = this.buildPromisesForGroupingAccounts(groupedAccountIds:string);

        Promise.all(promises).then(accountProfilePromises => {
            let accounts:Account[] = [];

            for(let accountProfiles of accountProfilePromises){
                for(let accounts of accountProfiles){
                    accounts.push(accounts);
                }
            }

            if(accounts.length < 1){
                reject(new NoAccountFoundError());
            }

            resolve(accounts);
        }).catch(err => {
            reject(err);
        });

    });
}

这是buildPromisesForGroupingAccounts,当还调用函数发送http请求时。

private buildPromisesForGroupingAccounts(groupedIds:string[]){
    let promises: Promise<Account[]>[] = [];

    for(let accountIdsBy100s of groupedIds){
        let get100Accounts = new Promise<Account[]>((resolve, reject) => {
            var path = 'accounts/?'+'accounts='+accountIdsBy100s;
            this.websiteRequest(path, reject, (r:any,body:any)=>{
                  let accountsJSON = JSON.parse(body).response.players;

                  let accounts:Account[] = [];

                  for(let account of accountsJSON){
                      accounts.push(account);
                  }

                  resolve(accounts);
            });
        });

        promises.push(get100Accounts);
    }

    return promises;
}

这是网站请求功能:

public websiteRequest(path:string, reject: Function, cb: Function){
    request.get({
        url:Server.SERVER_URL+path
      }, 
      async (err,r,body) => {
          if(err){
            reject(new FailedToConnectError());
          }else{
            cb(r,body);
          }
      });
}

最后,这是我运行代码时遇到的错误:

(node:3835) UnhandledPromiseRejectionWarning: Error: expect(value).toBeInstanceOf(constructor)
Expected constructor: FailedToConnectError
Received constructor: NoAccountFoundError
Received value: [Error: No account found]
(node:3835) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:3835) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

0 个答案:

没有答案