question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting 404 or 204 as responses; need guidance on using async / await

See original GitHub issue

Hello!

Total Koa newbie here, also new to the async/await syntax. In the post request below i was wondering how I can properly return the completed variable and attach it to ctx.body.

router.post('/resume', async (ctx) => {
  await form.parse(ctx.req, (err, fields, files) => {
    return sendMail(fields.candidateName, files.attachment)
      .then((completed) => {
        return completed;
      });
  });
})

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
PlasmaPowercommented, Aug 28, 2016

@rainesinternationaldev form.parse doesn’t return it a promise from what I can tell. It seems to use an event based system. Try this:

router.post('/resume', (ctx) => {
  ctx.body = '';
  return new Promise((resolve, reject) => {
    form.on('error', reject);
    form.on('close', resolve);
    form.parse(ctx.req, (err, fields, files) => {
      sendMail(fields.candidateName, files.attachment)
        .then((completed) => {
          ctx.body += completed + '\n';
        })
        .catch((err) => {
          ctx.body += 'ERROR: ' + err + '\n';
          ctx.status = 500;
        })
    });
  });
});
1reaction
PlasmaPowercommented, Aug 28, 2016

@rainesinternationaldev Thanks! 😃 About the moving of reject and resolve - I’d be a bit careful about that as this will now error to the console if you have more than one upload in your form (as the Promise will resolve multiple times).

I thought a response was triggered the moment you assign ctx.body

Nope, a response is triggered when the returned promise resolves. Also, now you can just set ctx.body instead of appending to it as your code is for one upload only.

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - I am getting 404 error and 204 error when ...
What i have tried: Frontend: I tried with adding headers to my axios request. I console logged the data and it seems perfectly...
Read more >
Should I return a 204 or a 404 response when a resource is ...
HTTP 204 means that something was found, but it's empty. For instance, imagine that you're serving log files through HTTP, with the requests ......
Read more >
HTTP/1.1: Status Code Definitions
Use of this response code is not required and is only appropriate when the response would otherwise be 200 (OK). 10.2.5 204 No...
Read more >
Minimal APIs quick reference - Microsoft Learn
Provides an overview of minimal APIs in ASP.NET Core.
Read more >
Using Express.js Routes for Promise-based Error Handling
And now let's use it in our users router in async / await style with direct response manipulation: router.get('/:id/profilePic', async function (req, res) ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found