0

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. 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. 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.

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.

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.

Is there any way to have the i value increase in correspondence with dbx.sharingGetSharedLinkFile().then()?


4

2 回答 2

0

要实现预期的行为,您需要使用函数而不是循环进行迭代。

您当前的代码不会像预期的那样工作,因为 dropbox api 方法的回调sharingGetSharedLinks仅在对 Dropbox 服务器(在此方法内部)的异步请求完成后才会运行,而主循环继续遍历文件列表。

最后,这都是关于承诺的,所以我尝试通过这个链接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();

            });

          }

        })()

}
于 2017-01-12T14:38:34.350 回答
0

你的代码真的很乱。如果您能够使用 es6 或q库。只需使用这样的承诺。

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
});

单独的每个请求的逻辑sharingGetSharedLinks返回这个请求的结果并处理它,filesGetTemporaryLink对结果做同样的事情sharingGetSharedLinks并发出一个新的http请求,然后最后一个重复这个过程,但结果是filesGetTemporaryLink

然后你必须能够一起处理所有的请求。[r1, r2, ...]其中 r1 包括对sharingGetSharedLinks、filesGetTemporaryLink 和sharingGetSharedLinkFile 的请求过程。

但是您必须重构所有代码,这是使用 Promises 的简单方法https://www.promisejs.org/patterns/

于 2017-01-12T14:43:27.593 回答