简体   繁体   English

如何正确履行诺言

[英]How to run a promise the correct way

I am not sure I am doing this correct. 我不确定我是否做对了。 I have a series of responses based off of certain variables set. 根据某些变量集,我有一系列响应。 I want to run a promise so after they have finished the app runs the next line of code. 我想兑现承诺,以便他们完成应用程序后再运行下一行代码。 here is my code: 这是我的代码:

const promises = []

if (this.score >= this.scoreNeeded) {
  this.correct = true
  this.retrieveCode(this.clueorder)
  this.feedback = 'Congrats you are moving on. Here is your next code '
} else {
  console.log('1')
  if (locimgface && !this.smile) {
    this.feedback += 'You need to smile in your picture. '
  }
  if (locimgfacesurprise && !this.surprise) {
    this.feedback += 'You need to look very surprised in your picture. '
  }
  if (locimgfacesorrow && !this.sorrow) {
    this.feedback += 'You need to look really sad in your picture. '
  }
  if (locationimagetexton && !this.atLocation) {
    this.feedback += 'We could not find the text in the image. It needs to have one of these words "' + locationimagetextwithspaces + '" somewhere in the picture. '
  }
  if (locationimagelabelon && !this.foundItem) {
    this.feedback += 'We could not find the item in the image. It needs to have one of these items "' + locationimagelabelswithspaces + '" somewhere in the picture. '
  }

  promises.push(this.feedback)

  Promise.all(promises).then(() => {
    console.log('completed from promise');
    this.deleteImage(this.fullPath)
  }).catch(err => {
    console.log(`ERROR: ${JSON.stringify(err)}`);
  })
}

What I am trying to do is make sure all the feedback messages are set properly and then run the deleteImage function, in addition in the block of code for a good score I would like to wait for a response from retrieveCode before posting the feedback. 我正在尝试做的是确保所有反馈消息均已正确设置,然后运行deleteImage函数,此外,在获得良好成绩的代码块中,我想在发布反馈之前等待retrieveCode的响应。

As always any and all help is much appreciated. 与往常一样,我们将不胜感激。

UPDATE: 更新:

I might be doing it wrong but there are multiple triggers. 我可能做错了,但是有多个触发器。

If the code is missing one or many it needs to let the user know what is missing, ie if the program is looking for a smile, and some text and a picture is submitted with the text but no smile the user should get a feedback msg letting them know they need to smile in the picture. 如果缺少一个或多个代码,则需要让用户知道缺少的内容,即程序正在寻找微笑,并且一些文本和图片与文本一起提交但没有微笑,则用户应该获得反馈信息让他们知道他们需要在图片中微笑。

If the program is looking for a surprised look, text and and a label and the user submits a pic with both a surprised look and the text they will get a feedback msg letting them know they need to include the label. 如果程序正在寻找令人惊讶的外观,文本和标签,并且用户提交的图片同时带有令人惊讶的外观和文本,则他们将收到反馈消息,告知他们需要包含标签。

If the program is looking for a smile, text and a label ('car') and the user submits a picture with none of these they will get a feedback msg saying they need to smile, they need to provide the missing text and they need to have a car in the picture. 如果程序正在寻找微笑,文本和标签(“汽车”),并且用户提交的图片都不包含这些内容,则他们会收到一条反馈信息,表示他们需要微笑,他们需要提供缺少的文本,并且他们需要在图片中有辆汽车。

For each item missing the user is provided specific feedback to help them along. 对于缺少的每个项目,都会向用户提供特定的反馈以帮助他们。

The end goal is to build the feedback message then run the next function. 最终目标是构建反馈消息,然后运行下一个功能。 either deleteImage() or retrieveCode() depending on the user's score deleteImage()或retrieveCode()取决于用户的得分

UPDATE 2 更新2

here is the deleteImage function 这是deleteImage函数

deleteImage (fullPath) {
        console.log('2')
        let storage = firebase.storage();
        let storageRef = storage.ref();

        // Create a reference to the file we want to delete
        let imageRef = storageRef.child(fullPath);
        imageRef.delete().then(() => {
            console.log('3')
            this.reset()
        })
        .catch((error) => {
            console.error(`file delete error occurred: ${error}`)
        })
    },

UPDATE 3: 更新3:

Here is the retrieveCode() function. 这是retrieveCode()函数。

retrieveCode(clueorder) {
    // get the next clue by adding 1 to this clue
    const newclue = this.getNextClue(clueorder)

    this.$store.dispatch('retrieveCode', {
        newclue: newclue,
        oldclue: clueorder
    });
}

From the MDN page: MDN页面:

The Promise.all() method returns a single Promise that resolves when all of the promises passed as an iterable have resolved or when the iterable contains no promises. Promise.all()方法返回一个Promise,当作为可迭代对象传递的所有promise已解决或可迭代对象不包含promise时,该Promise进行解析。

Basic Example 基本范例

Let's create a really basic function that returns a Promise (using Promise.resolve ), which returns the string which you pass it. 让我们创建一个真正的基本函数,该函数返回Promise (使用Promise.resolve ),该函数返回传递给它的字符串。

 // You could use new Promise(resolve => resolve(str)); const fetchData = str => Promise.resolve(str) // Array of promises to wait on const data = [ fetchData('foo'), fetchData('bar'), fetchData('baz') ]; Promise.all(data).then(responses => { console.log(responses); }); 

You don't have to return Promises either, you could simply pass an array of strings and Promise.all would "resolve" those. 您也不必返回Promises ,只需传递一个字符串数组即可,而Promise.all会“解析”那些字符串。

 // You could use new Promise(resolve => resolve(str)); const fetchData = str => str // Array of promises to wait on const data = [ fetchData('foo'), fetchData('bar'), fetchData('baz') ]; Promise.all(data).then(responses => { console.log(responses); }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM