简体   繁体   English

Javascript Async Await 函数要求

[英]Javascript Async Await function requirements

I have a project (html + JavaScript) created by other person.我有一个由其他人创建的项目(html + JavaScript)。 There is an image and a button on that html page.该 html 页面上有一个图像和一个按钮。 When I click the button, it calls a function and processes the image.当我单击按钮时,它会调用一个函数并处理图像。

My task is to process multiple images using that function.我的任务是使用该功能处理多个图像。 The images must be processed consecutive (not parallel), this is important.图像必须连续处理(不是平行处理),这一点很重要。

I use Async Await way to call the function, but the 'for loop' does not wait till the function finished processing the first image, it goes to the next images.我使用 Async Await 方式调用该函数,但是“for 循环”不会等到函数处理完第一张图像,它会转到下一张图像。

I tried to do this different ways (using promises etc.), but it does not work as expected.我尝试以不同的方式执行此操作(使用承诺等),但它没有按预期工作。 How can I force the 'for loop' to wait?我怎样才能强制'for循环'等待?

async function processImages(fileList) {
    for (let file of fileList) {
        await processOneImage(file);
    }            
}

async function processOneImage(filename) {
    // load an image
    document.getElementById('ImageToBeProcessed').src = filename;
    // call the function created by other person
    await document.querySelector('#start_process').click();
}

Is there some requirement for the.click() function to work with 'await' in a correct way? .click() 函数是否需要以正确的方式使用“await”? For example, .click() does not return anything now.例如,.click() 现在不返回任何内容。 Should I add我应该添加

return someVar

to the end of the.click() function?到.click() 函数的末尾?

Or maybe the.click() function should also have 'async' word before 'function'?或者 .click() 函数在“函数”之前也应该有“异步”这个词?

I looked at many async/await samples, but it did not help.我看了很多 async/await 示例,但没有帮助。

Thanks.谢谢。

PS More info:聚苯乙烯更多信息:

  1. That html+script use Google Polymer (perhaps it is important).该 html+script 使用 Google Polymer(也许它很重要)。 I shortened the.click() function code to show you.我缩短了 .click() 函数代码来向您展示。 Now it looks like:现在它看起来像:

     this.startProcessButton.addEventListener('click', function () { _this.prepareData().then(function () { // removed unneeded code here... _this.doProcessing() // removed unneeded code here... }).catch(function (error) { console.log(error); }); });

As I understand, it is a promise.据我了解,这是一个承诺。

  1. Unfortunately I can not contact that person.不幸的是我无法联系到那个人。 He/she does not answer any questions from Autumn.他/她不回答 Autumn 的任何问题。

To be able to await an operation, it needs to be async (or returning a promise ).为了能够等待一个操作,它需要是异步的(或返回一个promise )。 Furthermore, when you emit a click on the button, you only execute (and wait for completion) of the click, not the event handler.此外,当您点击按钮时,您只执行(并等待完成)点击,而不是事件处理程序。

You should create a function for processing one image (at the moment your processOneImage function doesn't process an image (,): it emits a click on a DOM button):您应该创建一个处理一个图像的函数(目前您的processOneImage函数不处理图像 (,):它发出对 DOM 按钮的点击):

function processOneImage(image) {
    return _this.prepareData()
        .then(function () {
        // removed unneeded code here ...
        _this.doProcessing()
        // removed unneeded code here ...
    })
        .catch(function (error) {
        console.log(error);
    });
}
  • please note that I've returned the promise!请注意,我已兑现承诺!

Then you can have it added to the event listener:然后你可以将它添加到事件侦听器中:

this.startProcessButton.addEventListener('click', (e) => processOneImage(getFileNameFromClickEvent(e));
  • separate your UI related concerns from your processing - getFileNameFromClickEvent can help figuring out how to get your file name or other input from the ui将与 UI 相关的问题与处理分开 - getFileNameFromClickEvent可以帮助弄清楚如何从 UI 获取文件名或其他输入

If you have this, you can reuse the processOneImage function (awaitable) w/o need of messing with DOM actions and emitting clicks.如果你有这个,你可以重用processOneImage函数(可等待)而无需处理 DOM 操作和发出点击。

async function processImages(fileList) {
    for (const file of fileList) {
        await processOneImage(file);
    }            
}

Note: this is a quick fix, you should consider following best practices (eg not mixing vanilla promises with async/await) and using a modern client framework/lib like React.js.注意:这是一个快速修复,您应该考虑遵循最佳实践(例如,不要将 vanilla promises 与 async/await 混合使用)并使用像 React.js 这样的现代客户端框架/库。

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

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