Dropzone是否可以在处理文件之前等待Promise解决?

huangapple go评论80阅读模式
英文:

Is it possible for Dropzone to wait for a promise to resolve before processing a file?

问题

I am using the latest version of Dropzone (currently v6) and I unable to find a way to make Dropzone wait for a promise to resolve before it starts processing a dropped file.

On the Dropzone's init function I have the following:

init: function () {
this.on("addedfile", someAsyncMethod);
this.on("processing", file => {
console.log("It always enters here before 'someAsyncMethod' promise resolves!");
});
}

Here is the someAsyncMethod code:

const someAsyncMethod = async () => {
const url = "https://my-service/api/some-method";
const result = await fetch(url);
// do some stuff here with the result
}

No matter what, the event handler for processing always starts before the on addedfile method handler resolves... Is it possible to make Dropzone wait for the onadded handler promise to resolve before it moves on to process the file added to the queue?

英文:

I am using the latest version of Dropzone (currently v6) and I unable to find a way to make Dropzone wait for a promise to resolve before it starts processing a dropped file.

On the Dropzone's init function I have the following:

init: function () {
    this.on("addedfile", someAsyncMethod);
    this.on("processing", file => {
        console.log("It always enters here before 'someAsyncMethod' promise resolves!");
    });
}

Here is the someAsyncMethod code:

const someAsyncMethod = async () => {
    const url = "https://my-service/api/some-method";
    const result = await fetch(url);
    // do some stuff here with the result
}

No matter what, the event handler for processing alway starts before the on addedfile mehod handler resolves... Is it possible to make Dropzone wait for the onadded handler promise to resolve before it moves on to process the fil added to the queue?

答案1

得分: 1

请考虑将autoProcessQueue选项设置为false。根据文档

如果您将选项autoProcessQueue设置为true,那么队列会在文件被拖放或上传完成后立即处理,通过调用calling.processQueue()来检查当前正在上传多少个文件,如果小于options.parallelUploads,则会调用.processFile(file)

如果将autoProcessQueue设置为false,则不会隐式调用.processQueue()。这意味着您需要在希望上传当前排队的所有文件时自己调用它。

这应该允许您在someAsyncMethod完成后调用处理。

英文:

Consider setting the autoProcessQueue option to false. As per the documentation:

> If you have the option autoProcessQueue set to true then the queue is immediately processed, after a file is dropped or an upload finished, by calling.processQueue() which checks how many files are currently uploading, and if it’s less than options.parallelUploads, .processFile(file) is called.
>
> If you set autoProcessQueue to false, then .processQueue() is never called implicitly. This means that you have to call it yourself when you want to upload all files currently queued.

This should allow you to invoke processing after someAsyncMethod completes.

huangapple
  • 本文由 发表于 2023年6月27日 21:45:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76565520.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定