-1
  const Firebird = require('node-firebird');
const { database } = require("../backend.conf");
async function pesqcdcidade(cidade) {
    return new Promise((resolve, reject) => {
        Firebird.attach(database, async function (err, db) {
            if (err) {
                reject(err);
            }
            const SQL = ' SELECT FIRST 1 CDCIDADE' +
                ' FROM CIDADES' +
                ' WHERE DESCRICAO =?';
            db.query(SQL, [cidade], async (err, resultParam) => {
                if (err) {
                    reject(err);
                };
                if (resultParam != undefined) {
                    if (resultParam.length > 0) {
                        for (k = 0; k < resultParam.length; k++) {
                            resolve(resultParam[k]);
                        }
                    } else {
                        resolve("inexistente")
                    }
                } else {
                    resolve("inexistente")
                }
                db.detach();
            });
        });
    });
};
async function insertCidade(endereco) {
    return new Promise((resolve, reject) => {
        Firebird.attach(database, function (err, db) {
            if (err) {
                reject(err);
            }
            const SQL = ' INSERT INTO CIDADES (CDCIDADE, DESCRICAO, CDESTADO) ' +
                ' VALUES ((SELECT COALESCE(MAX(CDCIDADE), 0) + 1 ' +
                ' FROM CIDADES), ?, ?)';
            db.transaction(Firebird.ISOLATION_READ_COMMITED, function (err, transaction) {
                if (err) {
                    reject(err);
                }
                transaction.query(SQL, [endereco.cidade, endereco.uf], function (err) {
                    if (err) {
                        reject(err);
                    }
                    transaction.commit(function (err) {
                        if (err) {
                            transaction.rollback();
                            reject(err);
                        } else {
                            resolve("OK")
                            db.detach();
                        }
                    });
                });
            })
        });
    });
};
module.exports = app => {
    const insert = async (req, res) => {
        let strTesteDescri = "cidade exemplo";
        let cdcidade = 0;
        let cliente = {
            "endereco": {
                "cidade": "exemplo de cidade",
                "uf": "RS"
            }
        }
        try {
            await pesqcdcidade(strTesteDescri)
                .then(async (resultado) => {
                    if (resultado == 'inexistente') {
                        return await insertCidade(cliente.endereco)
                            .then(
                                await pesqcdcidade(strTesteDescri)
                                    .then(resultado => {
                                        if (resultado != 'inexistente') {
                                            cdcidade = resultado.CDCIDADE
                                        }
                                    })
                            )
                            .catch((err) => {
                                throw new Error('insertCidade failed');
                            })
                    } else {
                        cdcidade = resultado.CDCIDADE
                    }
                })
        } catch (eer) {
            return res.status(500).send(err);
        }
    }
}`insira o código aqui`
Before anything, start by doing the tour to understand how the community works; then read the How to ask to see how you can improve your question. Stack Overflow Survival Guide in English.
– Gustavo André Richter
Valeime, reserve a second also to read about the use of Promises that will help you organize this code and eliminate these
thenunnecessary.– Leo Letto