【问题标题】:How can i catch after completing async function(webkitgetasentry, file upload)?完成异步功能(webkitgetasentry,文件上传)后如何捕捉?
【发布时间】:2017-11-21 09:52:47
【问题描述】:

我使用函数 webkitgetasentry 上传文件(包括文件夹) (拖放使用)

下面是我的代码

function drop(e){
    e.stopPropagation();
    e.preventDefault();
    //call start modal (waiting file uploading...)
    var items = e.dataTransfer.items;
    for(var i=0; i<items.length; i++){
        var item = items[i].webkitGetAsEntry();
        if (item){
            callmyfunction(item);
        }
    }
    //start modal close
}

callmyfunction 做文件上传。

但是这段代码是异步运行的。

所以在 callmyfunction 全部完成之前开始关闭模式

如何在 callmyfunction 全部完成后开始关闭模式?

【问题讨论】:

  • 将回调传递给 callmyfunction 并在其中启动模式关闭
  • 你可以让它同步。一种方法是使用promisesgenerators。或async function

标签: javascript html file-upload drag-and-drop modal-dialog


【解决方案1】:

callmyfunction async 函数应该接受一个回调参数,以便您可以在那里处理模态关闭。示例:

function callmyfunction(item, cb) {
    // do uploading...
    // execute cb after upload
    cb();
}

function drop(e){
    e.stopPropagation();
    e.preventDefault();
    //call start modal (waiting file uploading...)
    var items = e.dataTransfer.items;
    for(var i=0; i<items.length; i++){
        var item = items[i].webkitGetAsEntry();
        if (item){
            callmyfunction(item, function() { 
              // close modal
            });
        }
    }
}

另一种选择是使用Promise

【讨论】:

    猜你喜欢
    • 2021-06-02
    • 2019-10-04
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多