小编Mar*_*kee的帖子

在异步函数中,从回调函数返回值返回 Promise(undefined)

我是异步编程的新手,我面临着类似于这个问题的问题,在这个问题中建议的方法使用回调,但我正在尝试使用 Promises 和 async-await 函数来做到这一点。我在控制台中未定义。这是我的例子。我错过了什么?

 //Defining the function
 async query( sql, args ) {
    const rows = this.connection.query( sql, args, async( err, rows ) => 
     { 
        if ( err )
           throw new Error(err); 
        return rows; 
      } );
}

//calling the function here 
 db.query("select 1")
 .then((row) => console.log("Rows",row)) // Rows undefined
 .catch((e) => console.log(e));
Run Code Online (Sandbox Code Playgroud)

javascript callback node.js promise async-await

6
推荐指数
1
解决办法
5183
查看次数

标签 统计

async-await ×1

callback ×1

javascript ×1

node.js ×1

promise ×1