最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Wait for download method to finish before running next .then - node.js - Stack Overflow

programmeradmin1浏览0评论

I am trying to practice node by building a small application that allows a user to download a file.

I have saved the file on S3, saved the url in a mLab DB, and want to download the file to the app so I can use the url as an href on the client side. It currently works, to a point, but downloads an empty file. Which I think is due to the fact that it is running the next then() before the file has downloaded.

This is the code I currently have:

(async err => {
    const charge = await stripe.charges
      // create stripe charge
      .create({
       ...
      })
      // when the payment is successful, 
      // download the file locally to a 'dist' folder
      // so we can use it on the success page
      .then(() => {
        download(url_to_file, "dist").then(() => {
          console.log("done!");
        });
      })
      // then render the success page
      .then(
        res.render("success", {
          fileUrl: ...
        })
      )

So on the client I can (using ejs) do something like:

<a href="<%= /dist/fileUrl %>">Download</a>

I am very new to promises and not sure how to wait for the download method to pletely finish before running the next then and rendering the success page.

At the moment, the method runs, then the success page renders, then the file appears in the app. This allows the file to be downloaded but the file is empty.

Can anybody point me in the right direction / explain how to wait for the download to finish before running the next .then() method? Any help would be massively appreciated!

I am trying to practice node by building a small application that allows a user to download a file.

I have saved the file on S3, saved the url in a mLab DB, and want to download the file to the app so I can use the url as an href on the client side. It currently works, to a point, but downloads an empty file. Which I think is due to the fact that it is running the next then() before the file has downloaded.

This is the code I currently have:

(async err => {
    const charge = await stripe.charges
      // create stripe charge
      .create({
       ...
      })
      // when the payment is successful, 
      // download the file locally to a 'dist' folder
      // so we can use it on the success page
      .then(() => {
        download(url_to_file, "dist").then(() => {
          console.log("done!");
        });
      })
      // then render the success page
      .then(
        res.render("success", {
          fileUrl: ...
        })
      )

So on the client I can (using ejs) do something like:

<a href="<%= /dist/fileUrl %>">Download</a>

I am very new to promises and not sure how to wait for the download method to pletely finish before running the next then and rendering the success page.

At the moment, the method runs, then the success page renders, then the file appears in the app. This allows the file to be downloaded but the file is empty.

Can anybody point me in the right direction / explain how to wait for the download to finish before running the next .then() method? Any help would be massively appreciated!

Share Improve this question edited Mar 4, 2019 at 11:20 Mosh Feu 29.4k18 gold badges93 silver badges141 bronze badges asked Mar 4, 2019 at 9:20 Thomas AllenThomas Allen 8118 gold badges18 silver badges33 bronze badges 2
  • Try returning the call to download; e.g.: .then(() => { return download(url_to_file, "dist").then(() => { console.log("done!"); }); }) – Scott Rudiger Commented Mar 4, 2019 at 9:26
  • Hi @ScottRudiger, thanks for the reply. This downloads the file correctly, but the file is still empty when downloaded. – Thomas Allen Commented Mar 4, 2019 at 9:33
Add a ment  | 

1 Answer 1

Reset to default 3

If I'm not mistaking, just return the promise from the download.

Without return

Promise.resolve().then(()=> {
  new Promise(resolve => {
    setTimeout(() => {
      console.log('done 1');
      resolve();
    }, 1000);
  });
}).then(() => {
  console.log('done 2');
});

With return

Promise.resolve().then(()=> {
  return new Promise(resolve => {
    setTimeout(() => {
      console.log('done 1');
      resolve();
    }, 1000);
  });
}).then(() => {
  console.log('done 2');
});

发布评论

评论列表(0)

  1. 暂无评论