TheCoolestPenguin
TheCoolestPenguin

Reputation: 87

then() is not being triggered on a promise function

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

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)

Upvotes: 0

Views: 1222

Answers (1)

jfriend00
jfriend00

Reputation: 707218

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

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

Upvotes: 2

Related Questions