【问题标题】:Promise ignoring simple syncronous operationPromise 忽略简单的同步操作
【发布时间】:2023-01-24 16:56:40
【问题描述】:

在承诺中,我想为从一个类(在循环中)创建的几个对象的属性分配一个值,但是在执行函数并执行 .then(() => console.log(r)) 事情时,r 没有修改为承诺承诺的内容我会的。

这里:

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);
            }
        );
    }
    );
}

在线上

sentence.imageDescription = sentenceImageDescription;

我尝试为每个重复的句子分配图像描述,但是当我这样做时

assignSentenceImageDescription().then(r => console.log(r));

r 对象仍然没有将其每个 sentences 的 imageDescription 属性修改为数组 sentenceImageDescription 所具有的值,而这正是 assignSentenceImageDescription() 函数的目的。

【问题讨论】:

  • 因为 assigningWordsPartOFSpeech() 已经返回一个 Promise,将它包装在 Promise 构造函数中是一种反模式 - 你也是 resolve(r); ...这意味着函数 assignSentenceImageDescription 返回一个解析为原始 JSON 的 Promise,所以什么都没有你已经完成了那个 JSON 字符串的效果
  • @Bravo 在该函数中,如果我在没有 .the() 功能的情况下使其同步(而不是 resolve 我返回 r),那么我将得到未定义。有什么建议吗?
  • if I make it syncronous你不能让异步函数同步
  • @Bravo 有什么建议吗?请

标签: javascript


【解决方案1】:

按如下方式重构您的代码:

注意:您不需要 Promise 构造函数,因为 assigningWordsPartOFSpeech 返回一个您可以使用(并返回)的 Promise

设置sentences = JSON.parse(r).sentences;

现在你可以像你已经做的那样遍历句子,然后在 .then 中简单地 return sentences - 你就完成了

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;
    });
}

【讨论】:

  • 谢谢,我很感激时间,但是“r”对象包含更多内容,我希望修改后的句子对象仍然是一个“r”对象的一部分。如果我只返回句子,那么我只会得到一个只有句子的对象。我可以在之后嵌入它,但那将是非常意大利面条。
  • r 不是一个对象,它是一个字符串
  • @XaviFont - 我已经修改了代码,所以 data 现在是解析的 JSON - 即 data 是一个普通的 javascript 对象 - 现在你可以使用 data.sentences.forEach 来做你在问题中显示的内容。 .. 你想对响应做的任何其他事情都可以在 data 变量上完成,这是你可以在最后返回的内容。然后 - 我很抱歉回答问题中的代码,而不是其他一些你甚至没有显示的代码
  • 伟大的!!!有用。感谢您的解释。检查一下i.redd.it/thnmrz6us7491.png
猜你喜欢
  • 2017-03-04
  • 2018-06-24
  • 1970-01-01
  • 2013-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-27
  • 2018-04-10
相关资源
最近更新 更多