使用Firefox的Downloads.jsm一次下载一个文件,同时观察它们?

时间:2013-12-24 03:48:21

标签: javascript firefox firefox-addon download

Mozilla给了some sample code using Firefox's Downloads.jsm,我正在尝试使用Downloads.jsm以串行方式一次下载几个文件。

但是当我尝试使用它时:

Task.spawn(function () {

  let list = yield Downloads.getList(Downloads.ALL);

  let view = {
    onDownloadAdded: download => console.log("Added", download),
    onDownloadChanged: download => console.log("Changed", download),
    onDownloadRemoved: download => console.log("Removed", download),
  };

  yield list.addView(view);
  try {
    let download = yield Downloads.createDownload({
      source: "http://www.mozilla.org/",
      target: OS.Path.join(OS.Constants.Path.tmpDir, "example-download.html"),
    });
    list.add(download);
    download.start();
  } finally {
    yield list.removeView(view);

    moveOnToNextDownload();
  }

}).then(null, Components.utils.reportError);

我继续使用moveOnToNextDownload()进行下一次下载,开始下载并同时下载文件。

我应该如何使用Downloads.jsm以串行方式一次下载一个文件?

1 个答案:

答案 0 :(得分:2)

download.start是一个返回Promise的异步方法,因此在您首次下载启动后立即调用moveOnToNextDownload

相反,您可以执行download.start().then(moveOnToNextDownload)