我正在授权多方使用它的form.parse。它工作得很好,但是form.parse没有返回我可以使用的then/catch值的promise。
var Promise = require('bluebird');
var multiparty = Promise.promisifyAll(require('multiparty'), {multiArgs:true})
var form = new multiparty.Form();
form.parse({}).then((data)=>{console.log(data)});发布于 2018-07-12 17:03:07
以下是我使用内置Promise的解决方案:
const promisifyUpload = (req) => new Promise((resolve, reject) => {
const form = new multiparty.Form();
form.parse(req, function(err, fields, files) {
if (err) return reject(err);
return resolve([fields, files]);
});
});和用法:
const [fields, files] = await promisifyUpload(req)发布于 2019-07-05 05:57:12
我的解决方案是等待所有部分都读完:
const multipartParser = new Form();
multipartParser.on('error', error => { /* do something sensible */ });
const partLatches: Latch<void, Error>[] = [];
multipartParser.on('part', async part => {
// Latch must be created and pushed *before* any async/await activity!
const partLatch = createLatch();
partLatches.push(partLatch);
const bodyPart = await readPart(part);
// do something with the body part
partLatch.resolve();
});
const bodyLatch = createLatch();
multipartParser.on('close', () => {
logger.debug('Done parsing whole body');
bodyLatch.resolve();
});
multipartParser.parse(req);
await bodyLatch;
await Promise.all(partLatches.map(latch => latch.promise));在您想要进一步处理部件的情况下,这可能很方便,例如解析和验证它们,或者将它们存储在数据库中。
https://stackoverflow.com/questions/50522383
复制相似问题