Return promise when callback not provided
See original GitHub issueIn 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:
- Created 8 years ago
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Can we reopen this please? The latest express package already supports Promises.
You’re almost there! This should work:
Is this an acceptable solution?
I think that
pify
andthenify
works the same, whichever should work