nodejs:无法从主函数中的回调函数读取响应

nodejs: Not able to read response from callback function in the main function

这里是 nodejs 新手,仍在尝试学习 nodejs 的工作原理。您能否告诉我如何将“getDatafromCosmosDB”函数的回调响应传递到主函数中的变量并打印这些值。

当我尝试将 getDatafromCosmosDB 分配给变量“respdata”并尝试打印它时,它不起作用。

async function main(params, callback) {
    const logger = Core.Logger('main2', { level: params.LOG_LEVEL || 'info' })
    try {
        logger.info('main action')
        const respdata = getDatafromCosmosDB(function(response){
            console.debug(response)
            return response
        });
         console.debug(respdata)
    } catch (e) {
        console.debug(e)
    }
}

exports.main = main
async function getDatafromCosmosDB(callback){
    var query = new azure.TableQuery()
    .top(5)
    tableService.queryEntities('myCosmosTable', query, null, function (error, result, response) {
        if (!error) {
            console.log('success')
            return callback(result.entries)
        }
    });
}

尝试这样的事情,

import {
  createTableService,
  services,
  ServiceResponse,
  TableQuery
} from 'azure-storage';

getDatafromCosmosDB(): Promise<ServiceResponse> {
 return new Promise(async (resolve, reject) => {
       this.tableService.queryEntities(
        this.tableName,
        query,
        null,
        (error, _, response) => {
          if (!error) {
            resolve(response);
          } else {
            reject(error);
          }
        }
      );
    });
}

并像这样调用,

this.getDatafromCosmosDB().then(data => {
  console.log(data);
}