Skip to content

add async/await functionality - #248

Open
wootsaejao wants to merge 1 commit into
Hashnode:masterfrom
wootsaejao:add-async-await
Open

wootsaejao wants to merge 1 commit into
Hashnode:masterfrom
wootsaejao:add-async-await

Conversation

@wootsaejao

Copy link
Copy Markdown
Contributor

Related issue: #203

@wootsaejao wootsaejao mentioned this pull request Oct 11, 2016
@joefeser

Copy link
Copy Markdown

What about async support in the Post.Controller? I tried fighting that one and could not get it working over the weekend.

@wootsaejao

wootsaejao commented Oct 11, 2016 •

Copy link
Copy Markdown
Contributor Author

I tried playing with server/controllers/post.controller.js (did you mean this file?)

Changing from

export function getPosts(req, res) {
   Post.find().sort('-dateAdded').exec((err, posts) => {
     if (err) {
       res.status(500).send(err);
     }
     res.json({ posts });
   });
}

Into

export async function getPosts(req, res) {
  const queryPostsAsync = () => {
    return new Promise((resolve, reject) => {
      Post.find().sort('-dateAdded').exec((err, posts) => {
        if (err) {
          reject(err);
        }
        resolve({ posts });
      });
    });
  };

  try {
    const result = await queryPostsAsync();
    res.json(result);
  } catch (err) {
    res.status(500).send(err);
  }
}

And it still doing it's job.

Here is my commit de38cc5.

@mannyhenri
mannyhenri self-requested a review June 1, 2018 23:02
@mannyhenri mannyhenri added the v3.0.0 CLI/devtools optimization/improvements label Jun 1, 2018
@mannyhenri mannyhenri added this to the 3.0.0 milestone Jun 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v3.0.0 CLI/devtools optimization/improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants