Context.Done Azure function javascript multiple async calls

Deleted
Not applicable

 I have made an azure function with 3 calls to db. But as they work async only the first item gets added. My code below. So to be exact Context.Done but first, the calls have to be finished before going to the next call hope someone can help me. it seems it has to do with the azure function context as promises did not work either.

 

The code is for testing and need refactoring but first i want to make it work.

const rp = require('request-promise');
const azure = require("azure-storage");
var feed = require('feed-read');
const env = require('dotenv').config();
const con = "storageconnection";


function guid() {
    return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
        s4() + '-' + s4() + s4() + s4();
}
function CreateEntitie(item) {
    var link = item.link;
    var team = link.substr(39, link.length);
    team = team.substr(0, team.indexOf('/'));
    var newlink = link.substr(0, link.lastIndexOf('#'));
    var pubdate = item.published;
    var titel = item.title;
    var Entity = {
        PartitionKey: { '_': '' },
        RowKey: { '_': '0' },
        Gelezen: { "_": false },
        Team: { "_": team },
        Titel: { "_": item.title },
        Com_url: { "_": newlink },
        pubdate: { "_": item.published, '$': 'Edm.DateTime' }
    }
    return Entity;
}
function s4() {
    return Math.floor((1 + Math.random()) * 0x10000)
        .toString(16)
        .substring(1);
}
function getrownummer(azure, tableSvc, entitie) {
    for (var x = 0; x < entitie.length; x++) {
        var query = new azure.TableQuery()
            .where('com_url eq ?', entitie[x].Com_url._);
        //check if exists and update
        tableSvc.queryEntities('Posts', query, null, function (error, posts, response) {
            if (!error) {
                //update
                var rownummer = "0";
                for (var a = 0; a < posts.length; a++) {
                    rownummer = posts[a].RowKey._;
                }
                if (rownummer === "0") {
                    rownummer = guid();
                }
                entitie[x].RowKey._ = rownummer;


            }
        });
    }

    return entitie;
}
function InsertorReplaceItem(tableSvc, entitie) {
    var returnvalue;

    tableSvc.insertOrReplaceEntity('posts', entitie, function (error, inputed, response) {
        if (!error) {
              console.log(entitie.Com_url._ + "rownummer=" + entitie.RowKey._);
            // Entity updated
            return true;
        }
        else {
            return false;
        }
    });
}





module.exports = function (context, myTimer) {
    var timeStamp = new Date().toISOString();
    if (myTimer.isPastDue) {
        context.log('JavaScript is running late');
    }

    tableSvc = azure.createTableService(con);
    tableSvc.queryEntities('rssfeeds', null, null, function (error, result, response) {

        if (error) {
            context.log('well that didn\'t work: ' + err.stack);
            context.done();
        } else {
            context.log(result)
        };

        if (result) {
            var i = 1;
            var lastupdates = []
            feed(result.entries[i].rssurl._, function (err, articles) {
                if (err) throw err;
                var newdate = new Date(articles[0].published).getTime();
                for (var x = 0; x < articles.length; x++) {
                    //console.log(articles[x].title)
                    var from = new Date(articles[x].published).getTime();
                    var to = new Date(result.entries[i].Lastupdate._).getTime();
                    if (from >= to) {
                        var entitie = null;
                        var entitie = CreateEntitie(articles[x]);
                        lastupdates.push(entitie);
                        // context.log(entitie.titel);

                    }
                    if (newdate <= from) {
                        newdate = from;
                    }
                }
                context.log(lastupdates);
                var completeArticle = [];
                completeArticle = getrownummer(azure, tableSvc, lastupdates);
                context.log(completeArticle);
                if (completeArticle) {
                    for (var x = 0; x < completeArticle.length; x++) {
                        var endresult = InsertorReplaceItem(tableSvc, completeArticle[x]);
                       // context.log(endresult);
                    }

                    if (endresult) {
                        context.done;
                    }
                }
                //update date to latest date
            });
            // }

        }
    });
}

 

 

0 Replies