How does nodejs return data asynchronously?

The row of

asterisks will print out that the data that should be return in the undefined,
search function has the data I need, and there is no data on the
page. Where is the problem?

the data format of the page request is

async function search(searchtext) {
    var resdata = {};
    // 
    if (safetytest(searchtext)) {
        pool.getConnection(function (err, connection) {
            if (err) throw err;
            connection.query(
                "SELECT * FROM `***` where ***=?",
                searchtext,
                function (err, result) {
                    if (err) throw err;
                    resdata = {
                        uid: result[0].uid,
                        aname: result[0].aname,
                        lng: result[0].lng,
                        lat: result[0].lat,
                        address: result[0].address,
                        telephone: result[0].telephone
                    };
                    return resdata;
                }
            );
        });
    }
}
Mar.04,2021

pool.getConnection and connection.query if there is no promise/async, you can implement each promise manually.

function search(searchtext) {
    return new Promise((resolve, reject) => {
        var resdata = {};
        if (safetytest(searchtext)) {
            pool.getConnection(function (err, connection) {
                if (err) reject(err); // reject
                connection.query(
                    function (err, result) {
                        if (err) reject(err); // reject
                        resdata = {
                        };
                        resolve(resdata); // resolve
                    }
                );
            });
        }
    })
}

if there is, this may be the case.

async function search(searchtext) {
    var resdata = {};
    // 
    if (safetytest(searchtext)) {
        try {
            const connection = await pool.getConnection()
            const resdata = await connection.query()
            return resdata
        } catch (err) {
            throw err
        }
    }
}
Menu