Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

96
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!