The result of Promise is an array, how to call a Promise, to return the result of the final combination?

I am writing a background that encapsulates two methods to query the database, both of which return Promise, calls and both have the correct results.

function list(req, res, next) {
    let start = req.query.start;
    let count = req.query.count;
    exchangeDb.queryList(start, count).then(results => {
        res.json(results)
    }).catch(err => {
        throw err;
    });
}


function load(req, res, next, name) {
    let limit = req.query.limit;
    exchangeDb.queryVolume(name, limit).then(results => {
        req.results = util.convert(results);
        return next();
    }).catch(err => {
        throw err;
    });
}

the first one returns the list, and the second returns the details. I want to combine them into one method and try to do this:

function list(req, res, next) {
    let start = req.query.start;
    let count = req.query.count;
    exchangeDb.queryList(start, count).then(results => {
        results.forEach((index,element) => {
            exchangeDb.queryVolume(element.name,50).then(volume => {
                element.volume = volume;
                results[index] = element;
            })
        });
        res.json(results)
    }).catch(err => {
        throw err;
    });
}

obviously, it doesn"t work. I am deeply troubled by this problem. If chained Promise,then (). Then () doesn"t seem to solve this problem, please take a look at it. Thank you.

Mar.18,2021

it looks like you want to fetch the collection first and then the details of each element in the collection. At this time, because the operation of fetching details is also asynchronous, you cannot directly use forEach . Generally speaking, you have Promise.all and join into a queue.

Promise.all


queryVolumejsonres.json()

:<br>1.then

        results.forEach((index,element) => {
        
            const NOT_FILL = '__not_fill'
            
            // 
            const jsonResult = new Array(results.length)
            jsonResult.fill(NOT_FILL)
            
            exchangeDb.queryVolume(element.name,50).then(volume => {
                element.volume = volume;
                jsonResult[index] = element;
                
                // 
                if(!jsonResult.some(element => element === NOT_FILL)){
                    res.json(jsonResult)
                }
            })
        });

2. Encapsulate the process of the loop queryVolume into a new promise , the condition of resolve is the same as res.json of 1 , and then return json

in this promise's then method.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e512e1-45127.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e512e1-45127.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?