Node.js 如果异步函数中有错误,如何返回数字或错误?

Node.js 如果异步函数中有错误,如何返回数字或错误?,node.js,promise,async-await,Node.js,Promise,Async Await,我试图从一个异步函数中获得结果,但该异步函数可以返回承诺中的数字或错误(如果代码抛出) 我试图从catch块抛出异常。但是我得到了一些表达式所期望的TSLint错误 private async insertAppOrg(orgId): Promise<number> { try { return this.dbInstance.AppOrg.find({where: {orgId: orgId}}) .then(async (appOrg

我试图从一个异步函数中获得结果,但该异步函数可以返回承诺中的数字或错误(如果代码抛出)

我试图从catch块抛出异常。但是我得到了一些表达式所期望的TSLint错误

private async insertAppOrg(orgId): Promise<number> {
    try {
       return this.dbInstance.AppOrg.find({where: {orgId: orgId}})
            .then(async (appOrgData) => {
                if (appOrgData) {
                    return appOrgData.appOrgId;
                } else {
                    return (await this.createAppOrg(orgId)); //return number 
                }
        });
    } catch (ex) {
        return throw new ErrorFactory.DatabaseError(ex);
    }
}
private async insertAppOrg(orgId):承诺{
试一试{
返回此.dbInstance.AppOrg.find({where:{orgId:orgId}})
.然后(异步(appOrgData)=>{
如果(appOrgData){
返回appOrgData.appOrgId;
}否则{
return(等待this.createAppOrg(orgId));//返回编号
}
});
}捕获(ex){
返回抛出新的ErrorFactory.DatabaseError(ex);
}
}

如果成功,此函数应返回orgId(number),否则它应从catch块抛出异常。

您需要返回承诺,并删除try-catch块


private async insertAppOrg(orgId: number): Promise<number> {
  return this.dbInstance.AppOrg.find({ where: { orgId: orgId } })
      .then(async (appOrgData: any) => {
        if (appOrgData) {
          return appOrgData.appOrgId;
        } else {
          return (await this.createAppOrg(orgId));
        }
      })
      .catch((e: any) => {
        throw new Error("This is an error")
      });
}


私有异步insertAppOrg(orgId:number):承诺{
返回此.dbInstance.AppOrg.find({where:{orgId:orgId}})
.然后(异步(appOrgData:any)=>{
如果(appOrgData){
返回appOrgData.appOrgId;
}否则{
返回(等待这个.createAppOrg(orgId));
}
})
.catch((e:any)=>{
抛出新错误(“这是一个错误”)
});
}

您需要返回承诺,并删除try-catch块


private async insertAppOrg(orgId: number): Promise<number> {
  return this.dbInstance.AppOrg.find({ where: { orgId: orgId } })
      .then(async (appOrgData: any) => {
        if (appOrgData) {
          return appOrgData.appOrgId;
        } else {
          return (await this.createAppOrg(orgId));
        }
      })
      .catch((e: any) => {
        throw new Error("This is an error")
      });
}


私有异步insertAppOrg(orgId:number):承诺{
返回此.dbInstance.AppOrg.find({where:{orgId:orgId}})
.然后(异步(appOrgData:any)=>{
如果(appOrgData){
返回appOrgData.appOrgId;
}否则{
返回(等待这个.createAppOrg(orgId));
}
})
.catch((e:any)=>{
抛出新错误(“这是一个错误”)
});
}

返回throw
是语法错误,因为
throw
是语句,而不是表达式

另一个问题是,无法使用
async..await
中的
try..catch
处理来自返回承诺的错误,它应该是:

 return await this.dbInstance.AppOrg.find(...).then(...)
无需在
async
函数中使用
then
,因为
await
then
的语法糖:

private async insertAppOrg(orgId): Promise<number> {
    try {
       const appOrgData = await this.dbInstance.AppOrg.find({where: {orgId: orgId}});
        if (appOrgData) {
            return appOrgData.appOrgId;
        } else {
            return (await this.createAppOrg(orgId));
        }
    } catch (ex) {
        throw new ErrorFactory.DatabaseError(ex);
    }
}
private async insertAppOrg(orgId):承诺{
试一试{
const appOrgData=wait this.dbInstance.AppOrg.find({where:{orgId:orgId}});
如果(appOrgData){
返回appOrgData.appOrgId;
}否则{
返回(等待这个.createAppOrg(orgId));
}
}捕获(ex){
抛出新的ErrorFactory.DatabaseError(ex);
}
}

返回throw
是语法错误,因为
throw
是语句,而不是表达式

另一个问题是,无法使用
async..await
中的
try..catch
处理来自返回承诺的错误,它应该是:

 return await this.dbInstance.AppOrg.find(...).then(...)
无需在
async
函数中使用
then
,因为
await
then
的语法糖:

private async insertAppOrg(orgId): Promise<number> {
    try {
       const appOrgData = await this.dbInstance.AppOrg.find({where: {orgId: orgId}});
        if (appOrgData) {
            return appOrgData.appOrgId;
        } else {
            return (await this.createAppOrg(orgId));
        }
    } catch (ex) {
        throw new ErrorFactory.DatabaseError(ex);
    }
}
private async insertAppOrg(orgId):承诺{
试一试{
const appOrgData=wait this.dbInstance.AppOrg.find({where:{orgId:orgId}});
如果(appOrgData){
返回appOrgData.appOrgId;
}否则{
返回(等待这个.createAppOrg(orgId));
}
}捕获(ex){
抛出新的ErrorFactory.DatabaseError(ex);
}
}


至少,你的函数不会返回任何东西。它是任何类或模块的成员吗?是的,它是一个类的成员,我在同一个类中使用它。@bambam如果成功,它将返回一个数字承诺,如果有错误,则返回一个数字承诺。不,它不会返回。要返回,您需要
return
关键字。您是从某个内部函数返回的,而不是从
insertAppOrg
返回的。至少,您的函数不返回任何东西。它是任何类或模块的成员吗?是的,它是一个类的成员,并且在同一个类中使用它。@bambam如果成功,它将返回一个数字承诺,如果有错误,则返回一个数字承诺。否,它不返回。要返回,您需要
return
关键字。您是从某个内部函数返回的,而不是从
insertAppOrg
thoughYes我尝试了这个,但在本例中,IDE(TSLint)表示“声明类型既不是'void'也不是'any'的函数必须返回值”是的,我尝试了这个,但在本例中,IDE(TSLint)表示声明类型既不是“void”也不是“any”的函数必须返回值“@Prasanna有一点,原因已经解释清楚了。如果没有
wait
,您将无法通过
try..catch
捕获拒绝。谢谢您宝贵的回答。但我还有另一个疑问,在appOrgData常数中我会得到什么?根据我的说法,它可能返回结果或错误。在这两种情况下,我都会得到一些数据。“那么它会进入其他街区吗?”ManishJoshi我不确定我是否理解正确。您将在appOrgData中获得结果。这取决于你的数据库,它是什么样的结果。如果出现错误,它将直接转到上面帖子中的
catch
@estus,因为它不能正常工作。dbInstance.AppOrg.find()是sequalize函数,它不是异步的,所以如果我放置wait,它将无法工作。有什么解决办法吗?@ManishJoshi没有必要。它可以是
wait
ed。为了工作,它应该返回一个承诺。Sequelize长期使用承诺。@Prasanna有一点,原因已经解释清楚了。如果没有
wait
,您将无法通过
try..catch
捕获拒绝。谢谢您宝贵的回答。但我还有另一个疑问,在appOrgData常数中我会得到什么?根据我的说法,它可能返回结果或错误。在这两种情况下,我都会得到一些数据。“那么它会进入其他街区吗?”ManishJoshi我不确定我是否理解正确。您将在appOrgData中获得结果。这取决于你的数据库什么样的结果