Context.Done Azure函数Javascript多个异步调用

时间:2018-01-04 20:20:24

标签: javascript function azure asynchronous

我已经使用3次调用db创建了一个Azure函数。但是因为它们只是工作异步,所以只添加了第一项。 我的代码如下。所以准确地说Context.Done但首先,必须先完成调用才能进行下一次通话,希望有人可以帮助我。

它似乎与Azure功能上下文有关,因为承诺也不起作用。

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 个答案:

没有答案