Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

typescript - Problem with async/await service response with callback


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Wrap the code that you mentioned in padron.service with a promise like below. Now the value that is returned from the db is returned through passing it in resolve callback, and if there is some error, pass it in reject callback.

Note that in case of error, pass it to reject callback in asynchronous tasks as it will not give unexpected output in comparison to throwing the error

return new Promise((resolve, reject) => {
    fb.attach(this.options, (err, db) => {

        if (err) {
            // throw new Error('Erro na conexión á DB');
            reject('Erro na consulta ó Padrón');
        }

        db.query(query, params, (err, res) => {

            if (err) {
                // throw new Error('Erro na consulta ó Padrón');
                reject('Erro na consulta ó Padrón');
            }
            console.log('firebird.service - Response DB ================', res);

            db.detach();
            // return res
            resolve(res);
        });
    });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...