Javascript 何时延期

Javascript 何时延期,javascript,jquery,each,.when,Javascript,Jquery,Each,.when,我在一个数组中循环,当所有文件读取延迟被解析时,“完成”被记录 $.when.apply(null, $.each(files, function(index, file){ return self.fileRead.read(file).done(function(fileB64){ self.fileShow(file, fileB64, fileTemplate); }); })).done(function() {

我在一个数组中循环,当所有文件读取延迟被解析时,“完成”被记录

$.when.apply(null, $.each(files, function(index, file){

        return self.fileRead.read(file).done(function(fileB64){
            self.fileShow(file, fileB64, fileTemplate);
        });

    })).done(function() {  
        console.log('done');

    })
问题是,我只希望在fileShow方法返回后记录done

  • 这个fileShow方法是否也需要实现deferred。或者它能回来吗

  • 如何修改循环,以便在执行所有fileShow方法后运行console.log('done')


  • 使用
    $.map
    代替
    $.each
    。这将返回迭代函数返回的
    延迟的
    对象数组,然后可以将这些对象传递给
    $

    $.when.apply(null, $.map(function(index, file) {
        return self.fileRead.read(file).done(function(fileB64) {
            self.fileShow(file, fileB64, fileTemplate);
        });
    
    })).done(function() {  
        console.log('done');
    
    });
    

    使用
    $.map
    而不是
    $。每个
    都可以返回
    延迟的
    对象<代码>$。每个
    返回其第一个参数。这不起作用,它只是直接记录“完成”