【发布时间】:2015-11-08 18:01:33
【问题描述】:
我在我的应用程序中使用了 Promise 函数。下面附上我的代码:
module.exports = function (path)
{
return new Promise(function(resolve, reject)
{
fs.readFileAsync(path, encoding='UTF-8')
.then(function(data) {
return wmReuters.parseXMLtoJSON(data);
}).then(function(jsonedData) {
return new Promise(function(resolve, reject) {
resolve({
'name': jsonedData.newsMessage.itemSet[0].packageItem[0].contentMeta[0].slugline[0]['_'],
'description': jsonedData.newsMessage.itemSet[0].packageItem[0].contentMeta[0].headline[0]['_'],
'date': jsonedData.newsMessage.itemSet[0].packageItem[0].itemMeta[0].firstCreated[0],
'ingestDate': new Date(),
'lastModified': jsonedData.newsMessage.itemSet[0].packageItem[0].itemMeta[0].versionCreated[0],
'sourceId': jsonedData.newsMessage.itemSet[0].packageItem[0].contentMeta[0].altId[0]['_'],
'sourceNmae': 'Reuters',
});
});
}).catch(function(err) {
reject(new Error('Parsing Error: ' + err));
});
});
}
我在另一个文件中调用了这个函数(这个函数作为解析器导入)
parser('./Ingestor/XMLs/2014script/2014-01-01T000815Z_3_WNE9CUATJ_RTRWNEC_0_2210-UAE-DUBAI-NEW-YEAR-FIREWORKS.XML').then(function(obj) {
console.log(obj);
}).then(function(clip) {
console.log(clip);
}).catch(function(err) {console.log(err);})
解析器之后附加的第一个 then() 永远不会被触发。我想知道我的代码有什么问题(我猜 resolve 可能有问题,但我不确定在哪里)
【问题讨论】: