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.

Return promise when callback not provided

See original GitHub issue

In the example of custom error handling on the project page, it is

var upload = multer().single('avatar')

app.post('/profile', function (req, res) {
  upload(req, res, function (err) {
    if (err) {
      // An error occurred when uploading 
      return
    }

    // Everything went fine 
  })
})

I’m using ES7 async/await through Babel, and if the upload function returns a Promise when the error callback isn’t provided, it would be possible to have something like

var upload = multer().single('avatar')

app.post('/profile', wrap(async function (req, res) {
  try {
    await upload(req, res);
    // Everything went fine
    console.log(req.file);
  } catch(err) {
    // An error occurred when uploading 
    return
  }
}));

Or at least make it possible to use along with packages like https://www.npmjs.com/package/thenify or https://www.npmjs.com/package/thenify-all. I tried but couldn’t get it to work.

  var uploadsingle = require('thenify')(upload.single.bind(upload));
  await uploadsingle('file');  // It hangs at this statement

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

27reactions
xpepermintcommented, Oct 15, 2017

Can we reopen this please? The latest express package already supports Promises.

9reactions
LinusUcommented, Nov 9, 2015

You’re almost there! This should work:

const pify = require('pify')
const multer = require('multer')

const upload = pify(multer().single('avatar'))

app.post('/profile', wrap(async function (req, res) {
  try {
    await upload(req, res)
    // Everything went fine
    console.log(req.file)
  } catch(err) {
    // An error occurred when uploading 
    return
  }
}))

Is this an acceptable solution?

I think that pify and thenify works the same, whichever should work

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using promises - JavaScript - MDN Web Docs
Essentially, a promise is a returned object to which you attach callbacks, instead of passing callbacks into a function. Imagine a function, ...
Read more >
Return Promise if callback is not provided? #1515 - GitHub
I've thought about this, and experimented with it a bit. People are going to want to await Async.mapLimit(arr, 10, async foo => {...})...
Read more >
Converting Callbacks to Promises in Node.js - Stack Abuse
Let's talk about how to covert callbacks to promises if the util.promisify() function is not available. The idea is to create a new...
Read more >
How to make a Promise out of a Callback function in JavaScript
In the code above, I put the callback-based request function inside a Promise wrapper Promise( (resolve, reject) => { //callback function}) .
Read more >
Aren't promises just callbacks? - Stack Overflow
Promises are not callbacks. A promise represents the future result of an asynchronous operation. Of course, writing them the way you do, you...
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