小编use*_*943的帖子

等待Node.js中的多个回调

我有一个Node.js应用程序,可能会调用多个函数,具体取决于几个因素,但在最后一次回调后只调用一个最后一个函数.

这是我得到的简化版本:

if(foo === bar){
    function1(arg1, function(val1){
        doWhatever(val1, function(){
            res.end("Finished");
        });
    });
}else if(foo === baz){
    function2(arg2, function(val2){ 
        doWhatever(val2, function(){
            res.end("Finished");
        });
    });
}else{
    function3(arg3, function(val3){
        doWhatever(val3, function(){
            res.end("Finished");
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

这就是我正在做的事情:

var finished = false;

if(foo === bar){
    function1(arg1, function(val1){
        result = val1;
        finished = true;
    });
}else if(foo === baz){
    function2(arg2, function(val2){ 
        result = val2;
        finished = true;
    });
}else{
    function3(arg3, function(val3){
        result = val3;
        finished = true;
    });
}

var id = setInterval(function(){
    if(finished === true){ …
Run Code Online (Sandbox Code Playgroud)

node.js express

6
推荐指数
1
解决办法
6508
查看次数

把手里面的把手模板

我试图使用NodeJS和Backbone渲染Handlebars模板.

到目前为止,我在HTML文件中完成的工作非常有效:

app.MyView = Backbone.View.extend({
 ...    
 render: function() {           
   var template = Handlebars.compile( $("#my-template").html());        
   this.$el.html( template(this.model.toJSON()) );
   return this;
 }
 ...
});
Run Code Online (Sandbox Code Playgroud)

在HTML中查看:

<script type="text/x-handlebars-template" id="my-template">
   {{text}}
</script>
Run Code Online (Sandbox Code Playgroud)

但是,如果我将此视图放在Handlebars模板中,则它不起作用,因为NodeJS Handlebars编译器正在解释{{text}}.

node.js backbone.js handlebars.js

3
推荐指数
1
解决办法
1462
查看次数

标签 统计

node.js ×2

backbone.js ×1

express ×1

handlebars.js ×1