wrap node-multiparty with promisifyAll bluebird

582 views Asked by At

I'm new with node.js, I'm trying to wrap node-multiparty callbacks with bluebird but the parse function signature of multiparty is function(err, fields, files) and the promise signature requires just one return value. I'm sure there is a way to do this but I haven't found anything yet.

Thanks in advance!

2

There are 2 answers

0
victorkt On BEST ANSWER

Use spread instead of then. Taking from the example in their README:

var Promise = require('bluebird');
var multiparty = Promise.promisifyAll(require('multiparty'));
var http = require('http');
var util = require('util');

http.createServer(function(req, res) {
  if (req.url === '/upload' && req.method === 'POST') {
    // parse a file upload
    var form = new multiparty.Form();

    // USE `spread` INSTEAD OF `then` HERE 
    form.parseAsync(req).spread(function(fields, files) {
      res.writeHead(200, {'content-type': 'text/plain'});
      res.write('received upload:\n\n');
      res.end(util.inspect({fields: fields, files: files}));
    });

    return;
  }
}).listen(3000);
1
Alexander Yagovdik On

Had the same problem. Unfortunately victorkohl's solution does not work for me because of parse's callback pass 2 params (fields and files) Fixed by

    var multiparty = Promise.promisifyAll(require('multiparty'), {multiArgs:true})

Note: {multiArgs:true} option