deferred.promise( [target ] )
deferred.promise( [target ] )
설명 : 지연된 Promise 객체를 반환합니다.
이 deferred.promise()메서드는 비동기 함수로 인해 다른 코드가 내부 요청의 진행 또는 상태를 방해하지 못하도록합니다. 약속은 단지 연기 된 상태 (추가 핸들러를 첨부하거나 결정하는 데 필요한 방법 공개 then, done, fail, always, pipe, progress, state등을 promise),하지만 상태를 (변경 것들 resolve, reject, notify, resolveWith, rejectWith, 및 notifyWith).
If target가 제공되면 deferred.promise()메서드를 메서드에 연결 한 다음 새 메서드를 만드는 대신이 개체를 반환합니다. 이미 존재하는 개체에 약속 동작을 첨부하는 것이 유용 할 수 있습니다.
Deferred를 만드는 경우 Deferred에 대한 참조를 유지하여 어느 시점에서 해결되거나 거부 될 수 있도록합니다. Promise 객체 만 반환 deferred.promise()하면 다른 코드가 콜백을 등록하거나 현재 상태를 검사 할 수 있습니다.
자세한 내용은 지연 객체에 대한 설명서를 참조하십시오 .
예 :
Deferred를 만들고 두 개의 타이머 기반 함수를 설정하여 무작위 간격 후에 Deferred를 확인하거나 거부합니다. 어느 것이 든 먼저 불이 든다면 "이기고"콜백 중 하나를 부를 것이다. 첫 번째 타임 아웃 작업에서 Deferred가 이미 완료 되었기 때문에 (해결되거나 거부 된 상태에서) 두 번째 타임 아웃은 아무 효과가 없습니다. 또한 타이머 기반 진행 통지 함수를 설정하고 문서 본문에 "working ..."을 추가하는 진행 핸들러를 호출하십시오.
function asyncEvent() {
var dfd = jQuery.Deferred();
// Resolve after a random interval
setTimeout(function() {
dfd.resolve( "hurray" );
}, Math.floor( 400 + Math.random() * 2000 ) );
// Reject after a random interval
setTimeout(function() {
dfd.reject( "sorry" );
}, Math.floor( 400 + Math.random() * 2000 ) );
// Show a "working..." message every half-second
setTimeout(function working() {
if ( dfd.state() === "pending" ) {
dfd.notify( "working... " );
setTimeout( working, 500 );
}
}, 1 );
// Return the Promise so caller can't change the Deferred
return dfd.promise();
}
// Attach a done, fail, and progress handler for the asyncEvent
$.when( asyncEvent() ).then(
function( status ) {
alert( status + ", things are going well" );
},
function( status ) {
alert( status + ", you fail this time" );
},
function( status ) {
$( "body" ).append( status );
}
);
target 인수를 사용하여 기존 객체를 Promise로 승격 시키십시오.
// Existing object
var obj = {
hello: function( name ) {
alert( "Hello " + name );
}
},
// Create a Deferred
defer = $.Deferred();
// Set object as a promise
defer.promise( obj );
// Resolve the deferred
defer.resolve( "John" );
// Use the object as a Promise
obj.done(function( name ) {
obj.hello( name ); // Will alert "Hello John"
}).hello( "Karl" ); // Will alert "Hello Karl"
게시판 목록
개발자팁
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4736 | jQuery | 8년 전 | 1558 | ||
| 4735 | jQuery | 8년 전 | 1746 | ||
| 4734 | jQuery | 8년 전 | 1735 | ||
| 4733 | jQuery | 8년 전 | 2032 | ||
| 4732 | jQuery | 8년 전 | 1955 | ||
| 4731 | jQuery | 8년 전 | 1459 | ||
| 4730 | jQuery | 8년 전 | 1915 | ||
| 4729 | PHP |
|
8년 전 | 6898 | |
| 4728 | jQuery | 8년 전 | 2561 | ||
| 4727 | jQuery | 8년 전 | 1610 | ||
| 4726 | jQuery | 8년 전 | 1689 | ||
| 4725 | jQuery | 8년 전 | 1833 | ||
| 4724 | jQuery | 8년 전 | 1310 | ||
| 4723 | jQuery | 8년 전 | 1861 | ||
| 4722 | jQuery | 8년 전 | 1624 | ||
| 4721 | jQuery | 8년 전 | 1917 | ||
| 4720 | jQuery | 8년 전 | 1974 | ||
| 4719 | jQuery | 8년 전 | 1601 | ||
| 4718 | jQuery | 8년 전 | 1607 | ||
| 4717 | jQuery | 8년 전 | 1558 | ||
| 4716 | jQuery | 8년 전 | 1235 | ||
| 4715 | jQuery | 8년 전 | 1391 | ||
| 4714 | jQuery | 8년 전 | 1414 | ||
| 4713 | jQuery | 8년 전 | 1333 | ||
| 4712 | jQuery | 8년 전 | 2226 | ||
| 4711 | jQuery | 8년 전 | 1708 | ||
| 4710 | PHP |
|
8년 전 | 3747 | |
| 4709 | jQuery | 8년 전 | 1629 | ||
| 4708 | jQuery | 8년 전 | 1907 | ||
| 4707 | jQuery | 8년 전 | 1759 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기