如何从函数返回返回AngularJS保证?

时间:2016-05-16 17:54:21

标签: angularjs

我有这个Typescript / Javascript函数:

wordFormRowClicked = (wf): ng.IPromise<any> => {
    var self = this;
    if (this.wordFormIdentity != wf.wordFormIdentity) {
        angular.forEach(self.word.wordForms, function (wf, key) {
            var wordFormNgForm = 'wordFormNgForm_' + wf.wordFormIdentity;
            if (self[wordFormNgForm].$pristine) {
                ;
            } else {
                self.wordFormUpdate(wf).then((): any => {
                    self[wordFormNgForm].$setPristine();
                });
            }
        });
        this.wordFormIdentity = wf.wordFormIdentity;
    }
}

self.wordFormUpdate(wf)返回一个promise,因此给出了正确的返回类型,但是如果没有调用wordFormUpdate那么它不会返回一个promise,而且有可能会调用多个wordFormUpdates并且我必须确保它们在返回之前已经完成了。

有人可以建议我怎么做吗?

1 个答案:

答案 0 :(得分:3)

您的var _this = this; wordFormRowClicked = function (wf) { var self = _this; var promises = []; if (_this.wordFormIdentity != wf.wordFormIdentity) { angular.forEach(self.word.wordForms, function (wf, key) { var wordFormNgForm = 'wordFormNgForm_' + wf.wordFormIdentity; if (self[wordFormNgForm].$pristine) { ; } else { var updatePromise = self.wordFormUpdate(wf).then(function () { return self[wordFormNgForm].$setPristine(); }); promises.push(updatePromise); } }); } return $q.all(promises); }; wordFormRowClicked.then(function () { //at this point all promises are resolved this.wordFormIdentity = wf.wordFormIdentity; }); 函数必须返回一个承诺:

private void button3_Click(object sender, EventArgs e)
    {
        var jarRoot = @"..\..\..\..\paket-files\nlp.stanford.edu\stanford-parser-full-2015-12-09\models\";
        var modelsDirectory = jarRoot + @"\edu\stanford\nlp\models";

        // Loading english PCFG parser from file
        var lp = LexicalizedParser.loadModel(@"..\englishPCFG.ser.gz");

        // This option shows parsing a list of correctly tokenized words
        var sent = new[] { "This", "is", "an", "easy", "sentence", "." };
        var rawWords = Sentence.toCoreLabelList(sent);
        var parse = lp.apply(rawWords);
       // parse.pennPrint();

        // This option shows loading and using an explicit tokenizer
        const string Sent2 = "This is a test sentence.";
        var tokenizerFactory = PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
        var sent2Reader = new StringReader(Sent2);
        var rawWords2 = tokenizerFactory.getTokenizer(sent2Reader).tokenize();
        parse = lp.apply(rawWords2);

        var tlp = new PennTreebankLanguagePack();
        var gsf = tlp.grammaticalStructureFactory();
        var gs = gsf.newGrammaticalStructure(parse);
        var tdl = gs.typedDependenciesCCprocessed();
        //System.Console.WriteLine("\n{0}\n", tdl);



        var tp = new TreePrint("penn,typedDependenciesCollapsed");
        tp.printTree(parse);
            string s = "";
    }