【问题标题】:then() is not being triggered on a promise functionthen() 没有在 promise 函数上触发
【发布时间】: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 可能有问题,但我不确定在哪里)

【问题讨论】:

    标签: node.js promise bluebird


    【解决方案1】:

    您永远不会解决返回的最高承诺,因此调用者的 .then() 处理程序永远不会被调用。您可以通过返回 fs.readFileAsync() 来使用已有的承诺,而不是创建新的承诺,如下所示:

    module.exports = function (path) {
        return fs.readFileAsync(path, encoding='UTF-8')
        .then(function(data) {
            return wmReuters.parseXMLtoJSON(data);
            }).then(function(jsonedData) {
                return {
                    '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) {
            throw (new Error('Parsing Error: ' + err));
        });
    }
    

    仅供参考,sourceNmae: 'Reuters' 中可能有拼写错误。

    【讨论】:

    • 我通过在 catch(function(err)) 之后添加 .then(resolve, reject) 来解决它。不过,不错的收获!
    • @TheCoolestPenguin - .then(resolve, reject) 是一种反模式(浪费代码且不需要)。永远不需要它。如果您研究并复制我的答案,您会发现根本不需要创建自己的承诺。您可以只返回 fs.readFileAsync() 已经创建的那个,然后将您的其他操作链接到该承诺上。
    • 谢谢!并没有太多承诺。我现在明白了很多。
    猜你喜欢
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    • 2017-10-08
    • 2020-12-27
    • 1970-01-01
    • 2020-03-12
    • 2019-02-27
    • 2016-06-27
    相关资源
    最近更新 更多