>  기사  >  웹 프론트엔드  >  javascript는 연결된 비동기 호출 프레임워크 Async.Operation_javascript 기술을 지원합니다.

javascript는 연결된 비동기 호출 프레임워크 Async.Operation_javascript 기술을 지원합니다.

WBOY
WBOY원래의
2016-05-16 18:48:421375검색
复代码 代码如下:

Async = {};
Async.Operation = function(options) {
options = options || {};
var callbackQueue = [];
var chain = (options.chain && options.chain === true) ? 사실 : 거짓;
var 시작 = false;
var innerChain = null;
this.result = 정의되지 않음;
this.state = "실행 중";
this.completed = false;
this.yield = 함수(결과) {
var self = this;
if (!chain) {
self.result = 결과;
self.state = "완료";
self.completed = true;
} else {
시작됨 = true;
self.result = 결과;
self.state = "체인 실행";
self.completed = false;
}
setTimeout(function() {
if (!innerChain) {
while (callbackQueue.length > 0) {
var callback = callbackQueue.shift();
if (chain) {
callbackResult = callback(self.result);
self.result = callbackResult
if (callbackResult && callbackResult instanceof Async.Operation) {
innerChain = Async.chain() ;
while (callbackQueue.length > 0) {
innerChain.next(callbackQueue.shift())
}
innerChain.next(function(result) {
self.result = 결과;
self.state = "완료";
return result;
})
callbackResult.addCallback(function(result)) self.result = 결과;
innerChain.go(result);
})
}
} else {
callback(self.result)
}
}
if (!innerChain) {
self.state = "completed";
self.completed = true
}
} else {
while (callbackQueue.length > 0; ) {
innerChain.next(callbackQueue.shift());
}
innerChain.next(function(result) {
self.result = 결과;
self.state = "완료됨 ";
self.completed = true;
반환 결과;
});
}
}, 1);
이것을 돌려주세요;
};
this.go = function(initialArgument) {
return this.yield(initialArgument);
}
this.addCallback = function(callback) {
callbackQueue.push(callback);
if (this.completed || (체인 && 시작됨)) {
this.yield(this.result);
}
이것을 반환하세요.
};
this.next = function(nextFunction) {
return this.addCallback(nextFunction);
};
};
Async.chain = function(firstFunction) {
var chain = new Async.Operation({ chain: true });
if (firstFunction) {
chain.next(firstFunction);
}
반환 체인;
};
Async.go = function(initialArgument) {
return Async.chain().go(initialArgument);
}

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.