我正在写的Node.js应用程序是:我只是没有得到ES6“承诺”
- 接受文件(从HTTP“POST”),然后
- 将文件写入一个Box.com存储库。
这Node.js加载代码从HTTP读取文件数据“POST”消息,完美的作品:
// ORIGINAL (non-Promise; parse only)
app.post('/upload', function(req,res) {
console.log('/upload...');
var form = new multiparty.Form();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
的问题是,读取文件中的数据仅仅是几件事情第一我需要做的,所有这些都涉及异步回调。所以我想用promises序列化调用(错误的适当处理需要的话):
var Promise = require('bluebird');
...
// IDEAL SOLUTION (using "promises")
var data = {};
try {
parsePostMsg(req, data))
.then(sendAck(res, data))
.then(writeTempFile())
.then(sendToBox())
.then(deleteTempFile());
} catch (e) {
console.log("app.post(/upload) ERROR", e);
deleteTempFile();
}
问题:
的第一项功能,parsePostMsg()
,本身有回调。永远也不会被调用:
function parsePostMsg(req, data) {
console.log("parsePostMsg...");
return new Promise(function(resolve, reject) {
var form = new multiparty.Form();
form.parse(req, function(err, fields, files) {
data.fields = fields; // <-- This never gets called, so fields & files
data.files = files; // never get initialized!
});
});
}
问:我如何正确1)创建的承诺,然后2)调用parsePostMsg()
使3)form.parse()
正确地被沿链调用?
问:我甚至在正确的位置创建了“承诺”吗?
问:那么resolve()
和reject()
呢?它们在哪里适合?
============================================== ========================
附录:我已经尝试了很多东西,到目前为止没有任何工作。
目标:让这些功能(及其相关的回调,如适用)的顺序来运行:
- parsePostMsg(REQ数据)//等待“解析表单数据”回调来完成
- sendAck(RES,数据)//同步
- writeTempFile(数据)//建立的HTTP消息,发送,并等待来自远程服务器
- deleteTempFile(数据)//清除响应当一切都做
这里是一个失败的例子:
/*
* If the "timeout" values are the same (e.g. "10"), everything works fine.
*
* But if the timeout values are *different*, the order gets scrambled:
* Invoking parsePostMsg...
* ... OK ...
* Done: data= {}
* [email protected] data.temp { temp: 'foo' }
* [email protected] {} { temp: 'foo' }
* [email protected]: data.temp was foo
* [email protected] data.{fields, files} {} { temp: undefined, fields: [], files: [] }
*/
var Promise = require('bluebird');
var req = {}, res = {}, data = {};
var temp;
function parsePostMsg(req, data) {
console.log("parsePostMsg...");
return new Promise(function(resolve, reject) {
setTimeout(function() {
data.fields = [];
data.files = [];
console.log("[email protected] data.{fields, files}", req, data);
resolve();
}, 35);
});
}
function sendAck(req, data) {
console.log("sendAck...");
return new Promise(function(resolve, reject) {
setTimeout(function() {
console.log("[email protected]", req, data);
resolve();
}, 5);
});
}
function writeTempFile(data) {
console.log("writeTemp...");
return new Promise(function(resolve, reject) {
setTimeout(function() {
data.temp = "foo";
console.log("[email protected] data.temp", data);
resolve();
}, 2);
});
}
function deleteTempFile(data) {
console.log("deleteTemp...");
return new Promise(function(resolve, reject) {
setTimeout(function() {
console.log("[email protected]: data.temp was ", data.temp);
data.temp = undefined;
resolve();
}, 15);
});
}
console.log("Invoking parsePostMsg...");
parsePostMsg(req, data)
.then(sendAck(res, data))
.then(writeTempFile(data))
.then(deleteTempFile(data));
console.log("Done: data=", data);
入住文档.then'是什么'应该接受https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/然后 – zerkms
要使用promise,所有异步操作都需要“promisified”并返回一个promise在异步操作成功完成或拒绝时使用该值进行解析操作失败的原因。 – jfriend00
您需要从[如何将现有的回调API转换为承诺?](http://stackoverflow.com/q/22519784/1048572) – Bergi