Skip to content
This repository has been archived by the owner on May 22, 2021. It is now read-only.

[style] use async/await #325

Closed
dannycoates opened this issue Jul 26, 2017 · 6 comments
Closed

[style] use async/await #325

dannycoates opened this issue Jul 26, 2017 · 6 comments

Comments

@dannycoates
Copy link
Contributor

It might be fun to use async/await syntax on the server.

Here's a sample snippet:

app.get('/download/:id', async (req, res) => {
  const id = req.params.id;
  if (!validateID(id)) {
    return res.sendStatus(404);
  }
  try {
    const filename = await storage.filename(id);
    const contentLength = await storage.length(id);
    const timeToExpiry = await storage.ttl(id);
    res.render('download', {
      filename: decodeURIComponent(filename),
      filesize: bytes(contentLength),
      sizeInBytes: contentLength,
      timeToExpiry: timeToExpiry
    });
  } catch (e) {
    res.status(404).render('notfound');
  }
});
@pdehaan
Copy link
Collaborator

pdehaan commented Jul 27, 2017

💯

Related, doing something like #64 to convert Redis calls to a promise style instead of callback style, would make it play much better w/ async/await.
As would switching to the already promise-based fetch() API, circa #32 (although that's more of a front end optimization).

@ghost ghost added this to the Elegant Eagle (July 28) milestone Jul 27, 2017
@weihanglo
Copy link
Contributor

I changed all promises into async functions in express at weihanglo/send@bbe111a.

However, in storage.js, methods on Redis client seem cannot be promisified by NodeJS 8 util.promisify. I thought this issue may be related to 424eb2c. Any idea?

@dannycoates
Copy link
Contributor Author

@weihanglo thanks! Feel free to submit a PR and we can discuss the details there.

@pdehaan
Copy link
Collaborator

pdehaan commented Aug 3, 2017

Awesome work, @weihanglo!

There are a few more Promise notes buried in #64 (most notably the #64 (comment) which has a pointer to the official Redis+Promise documentation on using bluebird to promisify the entire RedisClient using something like:

bluebird.promisifyAll(redis.RedisClient.prototype);

I only briefly played with their sample code, but I'm sure we could figure it out if you need a second pair of eyes.

dannycoates added a commit that referenced this issue Aug 3, 2017
Use async function instead of promise (#325)
@gautamkrishnar
Copy link
Contributor

gautamkrishnar commented Aug 4, 2017

@pdehaan Can this issue be closed? I think it's fixed now...

@pdehaan
Copy link
Collaborator

pdehaan commented Aug 4, 2017

@gautamkrishnar I think we're still keeping this open until we try and promisify the server/storage.js bits.

@dannycoates dannycoates modified the milestones: Elegant Eagle, Stretch Aug 9, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants