简体   繁体   English

Dropbox JS SDK:在.then-function中执行i ++

[英]Dropbox JS SDK: execute i++ inside .then-function

I struggled somewhat naming this question, but I'll try to explain it rather quickly. 我在命名这个问题时有些挣扎,但我会尽力快速解释它。

I'm using the Dropbox JavaScript SDK to retrieve shared links from specific folders. 我正在使用Dropbox JavaScript SDK从特定文件夹中检索共享链接。 First of all, I'm using the following code to retrieve files from a folder: 首先,我正在使用以下代码从文件夹中检索文件:

var DROPBOX_PATH = path;
var ACCESS_TOKEN = '***';
var dbx = new Dropbox({ accessToken: ACCESS_TOKEN });

function listFiles() {
  dbx.filesListFolder({ path: DROPBOX_PATH })
    .then(function(response) {
      displayFiles(response.entries);
    })
    .catch(function(error) {
      console.error(error);
    });

  return false;
}

I will then loop through these files to use dbx.sharingGetSharedLinks() , dbx.filesGetTemporaryLink() as well as dbx.sharingGetSharedLinkFile() and get their links. 然后,我将遍历这些文件以使用dbx.sharingGetSharedLinks()dbx.filesGetTemporaryLink()以及dbx.sharingGetSharedLinkFile()并获取它们的链接。 The thing is, this process can take some time if the folder contains a couple of files and I want to use a <progress> element to show how far the progress has gone. 事实是,如果文件夹包含几个文件,并且我想使用<progress>元素来显示进度进展了多少,那么此过程可能需要一些时间。

First, I tried to use a for(var i = 0; i < files.length; i++) loop to wrap the three functions mentioned above, and add i to progress bar value under dbx.sharingGetSharedLinkFile() 's .then() process, but they're not in sync -- the i value is increased before all of those functions reach their respective ends and can output anything. 首先,我试图用一个for(var i = 0; i < files.length; i++)环包上述三个功能,并且添加i下于进度条值dbx.sharingGetSharedLinkFile().then()过程,但它们不同步-在所有这些函数到达各自的末端并可以输出任何内容之前, i值会增加。

I tried instead to replace it with a while() loop and put i++ inside the last dbx function, but that just makes my web browser go overload and possibly end up in an endless loop. 我尝试改为用while()循环替换它,并将i++放在最后一个dbx函数中,但这只会使我的Web浏览器过载,并可能陷入无休止的循环。

Is there any way to have the i value increase in correspondence with dbx.sharingGetSharedLinkFile().then() ? 有什么方法可以使dbx.sharingGetSharedLinkFile().then()对应的i值增加吗?


To achieve expected behaviour you need to iterate using a function instead of a cycle. 为了实现预期的行为,您需要使用函数而不是循环进行迭代。

Your current code wont work like expected because callback of dropbox api method sharingGetSharedLinks will run only after async request to a dropbox server (inside this method) will be done, while main loop continue iterating through the files list. 您当前的代码将无法按预期工作,因为Dropbox api方法sharingGetSharedLinks回调仅在完成对Dropbox服务器(此方法内部)的异步请求后才会运行,而主循环继续遍历文件列表。

Finally, it's all about promises so i try to point you in a right direction with this link MDN about Promises and little code example: 最后,所有内容都与承诺有关,因此我尝试通过有关承诺的链接MDN和少量代码示例为您指明正确的方向:

function displayFiles(files) {

        //... your code
        // i'll show you only changes

        var i = 0;

        (function listNextFile(){


          let file = files[i]

          if (file) {

            var name = file.name.slice(4).slice(0, -7).replace('_', '/'),
                file_path = file.path_display;

            dbx.sharingGetSharedLinks({ path : file_path }).then(function(response){

              // ...
              // there is your code
              // after all you need to icrease counter and call function again

              i++
              listNextFile();

            });

          }

        })()

}

Your code is really messy. 您的代码确实很乱。 If you're able to use es6, or q library. 如果您能够使用es6或q库。 Just use promises like this. 像这样使用诺言。

 var promises = []; files.forEach((file) => { promises.push(sharingGetSharedLinks(file_path).then(filesGetTemporaryLink(download_path)).then(sharingGetSharedLinkFile(download_url))); }); Promise.all(promises).then((values) => { // values are, an array of responses where each response is the three requests per each file }); 

Separate the logic for each request sharingGetSharedLinks returns the result for this request and process it, filesGetTemporaryLink do the same thing with the result of sharingGetSharedLinks and make a new http request, then the last one repeat the process but with the result of filesGetTemporaryLink . 对于每个请求,单独的逻辑sharingGetSharedLinks返回该请求的结果并进行处理, filesGetTemporaryLinksharingGetSharedLinks的结果执行相同的操作,并发出一个新的http请求,然后最后一个重复该过程,但对filesGetTemporaryLink

Then you have to be able to handle all the requests together. 然后,您必须能够一起处理所有请求。 [r1, r2, ...] where r1 includes the process of request to sharingGetSharedLinks, filesGetTemporaryLink, and sharingGetSharedLinkFile. [r1, r2, ...]其中r1包括对sharedGetSharedLinks,filesGetTemporaryLink和sharedGetSharedLinkFile的请求过程。

But you have to refactor all your code, here it's a how simply is to use Promises https://www.promisejs.org/patterns/ 但是您必须重构所有代码,这就是使用Promises https://www.promisejs.org/patterns/的简单方式

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM