使用回调查询多个promise

问题描述 投票:0回答:1

在node.js中,我有一个databaseMapper.js文件,该文件使用Ojai节点MapR api。提取数据。到目前为止,我可以处理单个文档,但是由于这是一个异步api,因此在查询多个文档时会遇到一些问题。

这是我到目前为止所拥有的:

function queryResultPromise(queryResult) {
//this should handle multiple promises
    return new Promise((resolve, reject) => {
        queryResult.on("data", resolve);
        // ...presumably something here to hook an error event and call `reject`...
    });
}



const getAllWithCondition = async (connectionString, tablename, condition) =>{
    const connection = await ConnectionManager.getConnection(connectionString);
    try {
        const newStore = await connection.getStore(tablename);
        const queryResult = await newStore.find(condition);
        return await queryResultPromise(queryResult);
    } finally {
        connection.close();
}
}

这里它将仅返回第一个,因为queryResultPromise将在第一个文档上显示resolve。但是,在查询结果将像"data"这样结束之前,queryResult.on('end', () => connection.close())的回调可能会发生多次。

我尝试使用类似Promise.all()的方法来解决所有问题,但我不确定如何将queryResult.on回调包含在此逻辑中

在node.js中,我有一个databaseMapper.js文件,该文件使用Ojai节点MapR api。提取数据。到目前为止,我可以使用单个文档,但是由于这是一个异步api,因此我有一些问题...

javascript node.js database asynchronous mapr
1个回答
0
投票

这将起作用

© www.soinside.com 2019 - 2024. All rights reserved.