1

I have a function which takes a url array and fetches the titles of those url's using npm package 'read-titles'. It successfully console.log the titles but does not push it into array.The array prints empty. The code is

function getTitleArray(addresses) {
    var titles = [];
    if (addresses.length > 1) {
        addresses.forEach((urls) => {

            console.log(urls.search('http'));
            if (urls.search('http://') == -1) {
                urls = 'http://' + urls;
            }

            readTitle(urls).then((title) => {
                titles.push(title);
                console.log(title);
            }, (err) => {
                res.status(400).send(err);
            });

        });
    }
    console.log('Titles are: ' + titles);
    return titles;
}
2

2 Answers 2

1

As @Mark_M said, you have an asynchronous code, so you have to make a callback system to access to titles when all readTitle calls are over.

This is a recursive way you can use :

function getTitleArray(addresses, callback) {

    var titles = [];
    if (addresses.length > 1) {

        let syncCallReadTile = function (it = 0, callback) {
            let url = addresses[it];
            if (url.indexOf('http://') == -1) {
                url = 'http://' + url;
            }
            readTitle(url).then((title) => {
                titles.push(title);
                it++;
                if (it == addresses.length) {
                    callback(null, titles);
                }
                else {
                    syncCallReadTile(it, callback);
                }
            }, callback);
        };

        syncCallReadTile(0, callback);

    }
}

getTitleArray([], (err, titles) => {
    if (err) {
        res.status(400).send(err);
    }
    else {
        console.log("Title :", titles);
    }   
})
Sign up to request clarification or add additional context in comments.

Comments

0

Since readTitle is asynchronous, empty array is returned as response, Here modified the snippet using Javascript callback. Try this one.

function getTitleArray(addresses, callback) {
    var titles = [];
    if (addresses.length > 1) {
        addresses.forEach((urls, index) => {

            console.log(urls.search('http'));
            if (urls.search('http://') == -1) {
                urls = 'http://' + urls;
            }

            readTitle(urls).then((title) => {
                titles.push(title);
                console.log(title);
                if((addresses.length - 1) === index) {
                    callback(null, titles);
                }               
            }, (err) => {
                callback(err)
            });
        });
    }
}

getTitleArray([], (err, titles) => {
    if(err) {
        res.status(400).send(err);
    } else {
        console.log("Title :", titles);
    }   
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.