Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

98
Vistas
Promise ignoring simple syncronous operation

In a promise, I wanna assign a value to the poroperty of an several objects created from a class (in a loop), but when executing the function and doing the .then(()=>console.log(r)) thing, the r does was not modified to what the promise promised me it would.

Here:

function assignSentenceImageDescription () {
    return new Promise((resolve, reject) =>
    {
        assigningWordsPartOFSpeech().then((r) => {

                JSON.parse(r).sentences.forEach((sentence) => {
                        let adjectiveBeforeNoun = [];
                        let sentenceImageDescription = [];
                        sentence.words.forEach((wordInSentence) => {
                            try {
                                if (wordInSentence.partOfSpeech[0].wordtype === "n.") {
                                    let imageDescription = adjectiveBeforeNoun.join('') + wordInSentence.partOfSpeech[0].word;
                                    sentenceImageDescription.push(imageDescription)
                                    adjectiveBeforeNoun = [];
                                } else if (wordInSentence.partOfSpeech[0].wordtype === "superl.") {
                                    adjectiveBeforeNoun.push(wordInSentence.partOfSpeech[0].word + " ")
                                }
                            } catch (e) {
                                console.log("===NOT IN DICTIONARY===")
                            }
                        })
                        sentence.imageDescription = sentenceImageDescription;
                    }
                )
                resolve(r);
            }
        );
    }
    );
}

On the line where it says sentence.imageDescription = sentenceImageDescription; I try to assign the image description of ech of the sentences iterrated through, but when I do assignSentenceImageDescription().then(r => console.log(r)); the r object still does not have each of its sentences's imageDescription property modified to the value the array sentenceImageDescription has, which is what the assignSentenceImageDescription() function is intended to do.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Refactor your code as follows:

Note: you don't need a Promise constructor since assigningWordsPartOFSpeech returns a Promise that you can work with (and return)

Set sentences = JSON.parse(r).sentences;

Now you can iterate through sentences as you already do, then simply return sentences in the .then - and you're done

function assignSentenceImageDescription() {
    return assigningWordsPartOFSpeech().then((r) => {
        const data = JSON.parse(r);
        data.sentences.forEach((sentence) => {
            let adjectiveBeforeNoun = [];
            let sentenceImageDescription = [];
            sentence.words.forEach((wordInSentence) => {
                try {
                    if (wordInSentence.partOfSpeech[0].wordtype === "n.") {
                        let imageDescription = adjectiveBeforeNoun.join('') + wordInSentence.partOfSpeech[0].word;
                        sentenceImageDescription.push(imageDescription)
                        adjectiveBeforeNoun = [];
                    } else if (wordInSentence.partOfSpeech[0].wordtype === "superl.") {
                        adjectiveBeforeNoun.push(wordInSentence.partOfSpeech[0].word + " ")
                    }
                } catch (e) {
                    console.log("===NOT IN DICTIONARY===")
                }
            })
            sentence.imageDescription = sentenceImageDescription;
        });
        return data;
    });
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda