Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.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
Javascript 承诺全部-未经处理的承诺拒绝_Javascript_Node.js_Typescript_Promise_Es6 Promise - Fatal编程技术网

Javascript 承诺全部-未经处理的承诺拒绝

Javascript 承诺全部-未经处理的承诺拒绝,javascript,node.js,typescript,promise,es6-promise,Javascript,Node.js,Typescript,Promise,Es6 Promise,执行一些TTD并尝试抛出一个自定义异常,我创建了一个名为FailedToConnectorRor的异常,但我一直得到我的NoAccountFoundError 这是我的测试: Server.SERVER_URL = 'http://www.test.com.fail'; server.getAccounts(['999999999']).catch(e=>{ expect(e).toBeInstanceOf(FailedToConnectError); }); 下一个函数的主要

执行一些TTD并尝试抛出一个自定义异常,我创建了一个名为FailedToConnectorRor的异常,但我一直得到我的NoAccountFoundError

这是我的测试:

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

server.getAccounts(['999999999']).catch(e=>{
    expect(e).toBeInstanceOf(FailedToConnectError);
});
下一个函数的主要功能是,我们将帐户ID分组为100个组,然后构建一个要运行的承诺数组。(API一次最多只能查询100个帐户):

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

(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.

请不要将
Promise.all()
包装在手动创建的Promise中。这是一个好主意。只要使用
promise.all()
返回并返回它的承诺,我看不出有什么明显的错误。尝试添加一些
console.log
语句,或使用调试器找出发生了什么。我首先将URL和回调参数记录在
websiteRequest
@jfriend00中,这样我就删除了手动创建的承诺,并返回了
promise.all()
。现在我的捕获在我的单元测试中起作用。但是,有没有办法让我的
then()
逻辑在返回之前运行?现在,如果我包含
,然后
逻辑并返回它,我会得到
未处理的PromisejectionWarning:未处理的承诺拒绝。此错误源于在没有catch块的异步函数中抛出,或者拒绝未使用.catch()处理的承诺。(拒绝id:1)
否。函数将在
运行之前很久返回。然后()
运行。这就是异步操作在Javascript中的工作方式。我不清楚是什么原因导致了未经处理的承诺被拒绝(或者我会写一个解释的答案)。您的
websiteRequest()
函数确实不好,可能是原因(我不知道)。如果您想在这里使用承诺(这是一个好主意),那么获取
请求承诺
库(围绕
请求
库的承诺包装器),并使用它返回的承诺,而不是传递拒绝处理程序和/或使用异步回调。这可能会解决问题。
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.