calling a function having callback in for loop

后端 未结 2 1142
失恋的感觉
失恋的感觉 2021-01-29 06:37

I want to run window.resolveLocalFileSystemURI(file,success,fail) in for loop passing different file entries and want to return resolved entries in arr

相关标签:
2条回答
  • 2021-01-29 07:01

    this is all based on all your functions being synchronous, and if not: be more specific what you're using. (there is no jQuery here yet your tags say jquery)

    function resolveFile(path) {
        var result;
        window.resolveLocalFileSystemURI(path, function(file) {
            result = file;
        });
        return file;
    }
    var resolvedFiles = filesarr.map(resolveFile);
    callback(resolvedFiles);
    
    0 讨论(0)
  • 2021-01-29 07:08

    There are multiple primary ways to attack a problem like this:

    1. Manual coding of an asynchronous loop
    2. Using promises to coordinate multiple async operations
    3. Using a library like Async to coordinate multiple async operations

    Here's the manual version:

    function getFiles(filesarr, doneCallback) {
        var results = new Array(filesarr.length);
        var errors = new Array(filesarr.length);
        var errorCnt = 0;
        var overallCnt = 0;
    
        function checkDone() {
            if (overallCnt === filesarr.length) {
                if (errorCount) {
                    doneCallback(errors, results);
                } else {
                    doneCallback(null, results);
                }
            }
        }
    
        for (var i = 0; i < filesarr.length; i++) {
            (function(index) {
                window.resolveLocalFileSystemURI(url, function (entry) {
                    results[index] = entry;
                    ++overallCnt;
                    checkDone();
                }, function (e) {
                    errors[index] = e;
                    ++errorCount;
                    ++overallCnt;
                    checkDone();
                });
            })(i);
        }
    }
    
    getFiles(filesarr, function(errArray, results) {
        if (errArray) {
            // go errors here
        } else {
            // process results
        }
    });
    

    And, here's a version that uses ES6 promises:

    // make a promisified function
    function resolveFile(url) {
        return new Promise(function(resolve, reject) {
            window.resolveLocalFileSystemURI(url, resolve, reject);
        });
    }
    
    function getFiles(filesarr) {
        var promises = [];
        for (var i = 0; i < filesarr.length; i++) {
            promises.push(resolveFile(filesarr[i]));
        }
        return Promise.all(promises);
    }
    
    getFiles(filesarr).then(function(results) {
        // process results here
    }, function(err) {
        // error here
    });
    
    0 讨论(0)
提交回复
热议问题