arevilla009
arevilla009

Reputation: 453

Error with promise without a catch block nodeJS

I'm new with nodeJS and I'm using a library to translate some text into another one, the library is called "translate-api" and I'm having continuously the same error even when I change the structure of the promise. The error is throwed with translate.getText(). Some idea?

const translate = require('translate-api');

const traduciendo = async () => {
    var trans = await translating.iniciar();
    // returns an string
    return trans;
}
traduciendo().then(function(data) {
    translate.getText(data,{to: 'es'}).then(function(text){
        console.log(text)
    });
}).catch(function(e) {
    console.log('Error: ', e.message)
});

enter image description here Thanks!

Upvotes: 0

Views: 49

Answers (1)

trincot
trincot

Reputation: 350137

The reason is that when translate.getText(data,{to: 'es'}) rejects, you don't have a catch handler for it.

You should flatten the promise chain so your catch handler can deal with that scenario too:

translating.iniciar().then(function(data) {
    return translate.getText(data, {to: 'es'});
}).then(function(text) {
    console.log('text: ', text);
    return text;
}).catch(function(e) {
    console.log('Error: ', e.message);
});

Now you will get the console log from your catch callback, which will show you what went wrong with the getText call.

Note that I did not include traduciendo here, as it adds nothing to what translating.iniciar already does.

Upvotes: 1

Related Questions