then()没有在promise函数上触发 [英] then() is not being triggered on a promise function

查看:79
本文介绍了then()没有在promise函数上触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用了Promise函数.以下是我的代码:

I am using promise function in my application. Attached below is my code:

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));
    });
});

}

然后我在另一个文件中调用了此函数(此函数作为解析器导入)

and I called this function in another file (this function is imported as parser)

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()永远不会触发.我想知道我的代码出了什么问题(我猜解析可能有问题,但我不确定在哪里)

The first then() attached after parser is never triggered. I wonder what is wrong with my code( There is probably something wrong with resolve I guess but I am not sure where)

推荐答案

您永远都不会解决返回的最高承诺,因此永远不会调用调用方的 .then()处理程序.除了创建新的Promise,您还可以通过返回 fs.readFileAsync() Promise像这样使用已经拥有的Promise:

You are never resolving the top most promise that is returned so the caller's .then() handler never gets called. Instead of creating new promises, you can just use the ones you already have by returning the fs.readFileAsync() promise have like this:

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'中拼写错误.

FYI, you may have a misspelling in sourceNmae: 'Reuters'.

这篇关于then()没有在promise函数上触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆