2016-08-04 2 views
1

나는 assinged .then 함수와 함수를 반환하는 다음과 같은 함수가 있습니다.체인 동작을 Jquery .then

Func = function() { 
    return Func1([a, b])() 
     .then(_.bind(function() { 
      //something 
     }, this)); 
    }; 

그리고 나중에이 같은 Func 또 다른 .then 기능 할당 :

Func2 = function(){ 
    //something 
    this.Func() 
     .then(_.bind(function() { 
      //something 
     }, this)); 
    }; 

Func1().then().then() 또는 뭔가 다른 같은 체인이있을 것입니까?

+0

예. 예. '.then(). then(). then()'을 여러 번 사용할 수 있습니다. –

답변

0

이것은 promise입니다. 그리고 약속대로 많은 것을 할 수 있습니다. then! ;)

function callback() { 
    console.log("then"); 
} 

var p = new Promise(function(resolve) { 
    resolve(); 
}).then(callback); 

p.then(callback); 
p.then(callback).then(callback); 
p.then(callback).then(callback).then(callback); 

Working example.

아니면 예를 들어 같은 :

var Func1 = function(param1) { 
    return new Promise(function(resolve) { 
     resolve(); 
    }); 
} 

var Func = function() { 
    return Func1(["a", "b"]).then(function() { 
     console.log("then"); 
    }.bind(this)); 
}; 

var Func2 = function() { 
    Func().then(function() { 
     console.log("then"); 
    }.bind(this)) 
    .then(function() { 
     console.log("then chain"); 
    }); 
}; 

Func2(); 

Working example.

0

이 :

Func2 = function(){ 
    //something 
    this.Func() 
     .then(_.bind(function() { 
      //something 
      }, this)) 
     }, this)); 
    }; 

은 (는) 잘못된 JavaScript입니다. 열린 중괄호를 닫힌 중괄호에 대해 세어보십시오. 따라서 마지막 문장에서 오류가 발생합니다. .then() 체인을 원한다면 :

Func2 = function(){ 
//something 
this.Func() 
    .then(_.bind(function() { 
     //something 
    }, this)) 
    .then(someOtherFuncName) 
    .then(function() { 
     console.log("It's a..."); 
    }) 
    .then(function() { 
     console.log("..CHAIN REEEAAACTIONNNN"); 
    }); 
}; 
+0

다른 코드도 있으므로 질문 닫기 괄호를 게시하는 것이 반드시 닫히는 것은 아닙니다. 그래서, 당신의 대답은 말이되지 않습니다. –

+0

@BhojendraNepal 나쁜 코드를 게시하면 안됩니다. 게다가, 그는';'로 줄을 끝냅니다. 따라서 원래 코드 문서에 더 많은 중괄호가있는 경우에도 해당 문장 내에 중괄호가 더 이상 존재하지 않을 것입니다. 잘못된 JavaScript입니다. –

+0

그 대답은 대답이 아닌 것 같습니다. –