嵌套 Promise 不会将错误传播到 Node.js 中的父 Promise? [英] Nested Promise is not propagating error to parent Promise in Node.js?

查看:14
本文介绍了嵌套 Promise 不会将错误传播到 Node.js 中的父 Promise?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用运行 Express 的 Node.js/TypeScript 创建一个 API.以下是我的 get 方法的摘录.在 format 方法中触发了一个错误,它抛出了一个错误,该错误被 Promise 捕获,但在 throw 后没有传播到父 Promise:

I'm creating an API using Node.js/TypeScript running Express. Below is an excerpt from my get method. An error is being triggered in the format method, which throws an error that is caught by the promise, but not propagated to the parent promise after a throw:

            this.getModel(objectName).findAll(queryParameters).then(function(databaseObjects) {
                for (let databaseObject of databaseObjects) {
                    var jsonObject = {};
                    //console.log("Database object: ");
                    //console.log(databaseObject);
                    transform.baseFormat(databaseObject, jsonObject)
                    .then(() => transform.format(databaseObject, jsonObject))
                    .then(() => {
                        res.locals.retval.addData(jsonObject);
                    }).catch((e) => {
                        console.log("Caught error during format of existing object: ");
                        console.log(e);
                        throw e;
                    });
                }
            })
            .then(() => {
                if (metadata) {
                    this.metadata(objectName, false, transform, res.locals.retval);

                    delete queryParameters.limit;
                    delete queryParameters.offset;
                    console.log("RUNNING METADATA COUNT: ");
                    this.getModel(objectName).count(queryParameters).then(function(count) {
                        res.locals.retval.setMetadata("records", count);
                        return next();
                    }).catch(function(e) {
                        this.error(e, res);
                        return next();
                    });
                } else {
                    console.log("NO METADATA");
                    return next();
                }
            })
            .catch((e) => {
                // TODO: Move status into error() function
                console.log("500 Error on GET");
                console.error(e);
                res.locals.retval.addError(ErrorCode.InternalError, e);
                res.status(ErrorCode.InternalError).send(res.locals.retval);
                return next();
            });

这是输出:

(node:8277) Warning: a promise was created in a handler at /Library/WebServer/adstudio/dist/server.js:555:51 but was not returned from it, see
at Function.Promise.bind (/Library/WebServer/adstudio/node_modules/bluebird/js/release/bind.js:65:20)
Caught error during format of existing object: 
Test Error
END FUNCTION HAS BEEN REACHED!

然后请求无法完成.

我已经阅读了很多关于 Promises 的内容,但我无法找到与我类似的问题/解决方案.

I've read a lot on Promises and I haven't been able to find an issue/solution similar to mine.

推荐答案

在 for 循环中运行不是异步的,所以你的 promise 基本上是在循环完成后立即解决,但在所有格式化完成之前.

Running inside that for-loop is not asynchronous, so your promise is resolving basically as soon as the loop finishes, yet before all your formatting finishes.

使用承诺控制流程,例如 bluebird 的 Promise.each串行或只是 Promise.all.然后将捕获任何异常.

Use a promise control flow, like bluebird's Promise.each which is serial or just Promise.all. Then any exceptions will be caught.

this.getModel(objectName).findAll(queryParameters).then(function (databaseObjects) {
  var promises = databaseObjects.map(databaseObject => {
    var jsonObject = {}
          // console.log("Database object: ");
          // console.log(databaseObject);
    return transform.baseFormat(databaseObject, jsonObject)
          .then(() => transform.format(databaseObject, jsonObject))
          .then(() => {
            res.locals.retval.addData(jsonObject)
          }).catch((e) => {
            console.log('Caught error during format of existing object: ')
            console.log(e)
            throw e
          })
  })
  return Promise.all(promises)
})
.catch((e) => {
    // TODO: Move status into error() function
  console.log('500 Error on GET')
  console.error(e)
  res.locals.retval.addError(ErrorCode.InternalError, e)
  res.status(ErrorCode.InternalError).send(res.locals.retval)
  return next()
})

这篇关于嵌套 Promise 不会将错误传播到 Node.js 中的父 Promise?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆