當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript Promise.defer方法代碼示例

本文整理匯總了TypeScript中ts-promise.Promise.defer方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Promise.defer方法的具體用法?TypeScript Promise.defer怎麽用?TypeScript Promise.defer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在ts-promise.Promise的用法示例。


在下文中一共展示了Promise.defer方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1:

			(chunk: any): void|Promise<void> => {
				// Try to push data, returns true if there's still space
				if (this.push(chunk)) {
					return;
				}
				// Stream blocked, wait until _read() is called
				let d = Promise.defer();
				this._resumer = d.resolve;
				return d.promise;
			},
開發者ID:BuddyPlatform,項目名稱:ts-stream,代碼行數:10,代碼來源:node.ts

示例2: handleNodeStreamError

export function pipeToNodeStream<T>(
	tsReadable: Readable<T>,
	nodeWritable: NodeJS.WritableStream,
	emitError: boolean = false
): Promise<void> {
	let endDeferred = Promise.defer();
	let blockedDeferred: VoidDeferred;

	// Handle errors emitted by node stream: abort ts-stream
	function handleNodeStreamError(error: Error): void {
		// Don't 're-emit' the same error on which we were triggered
		emitError = false;
		// Make sure stream's end result reflects error
		endDeferred.reject(error);
		tsReadable.abort(error);

		if (blockedDeferred) {
			blockedDeferred.reject(error);
			nodeWritable.removeListener("drain", blockedDeferred.resolve);
		}
	}

	// Optionally pass ts-stream errors to node stream
	function handleTsStreamError(error: Error): void {
		if (!emitError) {
			return;
		}
		emitError = false;
		nodeWritable.removeListener("error", handleNodeStreamError); // prevent abort
		nodeWritable.emit("error", error);
	}

	nodeWritable.once("error", handleNodeStreamError);
	nodeWritable.once("finish", () => {
		nodeWritable.removeListener("error", handleNodeStreamError);
		endDeferred.resolve(); // ignored if error happens earlier
	});

	return tsReadable.forEach(
		(chunk: any): void|Promise<void> => {
			blockedDeferred = undefined;
			// Try to push data, returns true if there's still space
			let canAcceptMore = nodeWritable.write(chunk);
			if (!canAcceptMore) {
				// Stream blocked, wait until drain is emitted
				blockedDeferred = Promise.defer();
				nodeWritable.once("drain", blockedDeferred.resolve);
				return blockedDeferred.promise;
			}
		},
		(endError?: Error): Promise<void>  => {
			if (endError) {
				handleTsStreamError(endError);
			}
			// Note: we don't pass a callback to end(), as some types of
			// streams (e.g. sockets) may forget to call the callback in
			// some scenarios (e.g. connection reset).
			// We use the "finish" event to mark the stream's end, but it
			// doesn't get emitted on e.g. a connection refused error.
			nodeWritable.end();
			return endDeferred.promise;
		},
		handleTsStreamError // abort handler
	);
}
開發者ID:BuddyPlatform,項目名稱:ts-stream,代碼行數:65,代碼來源:node.ts


注:本文中的ts-promise.Promise.defer方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。