简体   繁体   English

在循环中的所有异步调用完成后如何调用 function?

[英]How to call a function after all async calls in loop are done?

The reader.onloadend calls a async function addData and reader.onloadend is in a loop. reader.onloadend 调用异步 function addData 并且 reader.onloadend 处于循环中。

const uploadFiles = () => {
    console.log(acceptedFiles)
    setLoading(true)
    console.log(loading)
    let i = 0
    let l = acceptedFiles.length

    for (i = 0; i < l; i++) {
      let file = acceptedFiles[i]
      const reader = new window.FileReader()
      reader.readAsArrayBuffer(file)
      reader.onloadend = () => {
        let buffer = Buffer(reader.result)
        console.log(buffer)
        addData(file.path, buffer)
      }
    }
  }

This is my async function addData这是我的异步 function addData

async function addData(name, buffer) {
    const file = await myfunction()
    
  }

I want to call setLoading(false) after all the async addData in the loop has been called.在调用循环中的所有异步 addData 之后,我想调用 setLoading(false) 。 The onClick function doesn't work, loading is set to false when I click the button before all the async function is done. onClick function 不起作用,当我在所有异步 function 完成之前单击按钮时,加载设置为 false。

const [loading, setLoading] = useState(false)
<button onClick={() => {
                uploadFiles()
                 setLoading(false) 
             }}
            disabled={loading}
          >

Using promises you can know when all of the files have been read.使用 Promise,您可以知道何时读取了所有文件。 Basic idea below基本思路如下

 const uploadFiles = (evt) => { // set loading to true; const acceptedFiles = [...evt.target.files]; // Create a map of promises that will resolve when file is read const readerPromises = acceptedFiles.map(file => { return new Promise(resolve => { const reader = new window.FileReader() reader.readAsArrayBuffer(file) reader.onloadend = () => { console.log(file); let buffer = reader; // Buffer(reader.result) resolve([file.name, buffer]) } }); }); // Detects when all of the promises are fully loaded Promise.all(readerPromises).then(results => { console.log(results); // set loading to false; }); };
 <input type="file" multiple onchange="uploadFiles(event)" />

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

相关问题 Javascript:在所有异步调用返回后调用函数 - Javascript: Call a function after all async calls have returned 在 foreach 循环中调用异步 function 并在循环完成后返回数组 - Call an async function in foreach loop and return array once loop is done 如何在完成所有setTimeout函数后调用函数 - How to call a function after all setTimeout functions is done 完成所有`transition`之后如何调用`callback`函数? - How to call a `callback` function after all `transition` done? 在for循环内的NodeJS递归调用中,如何知道何时完成所有调用? - NodeJS recursive call inside for loop, how to know when all calls are done? 完成后如何获取多个异步调用的返回值? - How to get return value of multiple async calls when it is all done? for循环中的几个异步调用,需要知道何时使用可观察对象完成所有调用 - Several async calls in the for-loop, need to know when all calls are done using observables 异步函数完成所有异步调用后,事件循环是否再次更改? - Does event loop changes again after async function finishes all asynchronous calls? 如何在循环内完成异步函数后调用函数? - How to call function after completion of async functions inside loop? 如何进行多个异步调用,然后在所有调用完成后执行一个函数? - How do I make multiple async calls, then execute a function after all calls have completed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM