瀏覽代碼

Introduced the Promise class and module.

fredck 11 年之前
父節點
當前提交
b36531fbec
共有 2 個文件被更改,包括 56 次插入0 次删除
  1. 27 0
      packages/ckeditor5-ui/src/promise.js
  2. 29 0
      packages/ckeditor5-ui/tests/promise/promise.js

+ 27 - 0
packages/ckeditor5-ui/src/promise.js

@@ -0,0 +1,27 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* globals window */
+
+'use strict';
+
+/**
+ * A ES6 compatible Promise class, used for deferred and asynchronous computations.
+ *
+ * @class Promise
+ */
+
+CKEDITOR.define( function() {
+	// For now we're using the native browser implementation of Promise, a ES6 feature. Just IE is not supporting it so
+	// a polyfill will have to be developed for it.
+	//
+	// http://caniuse.com/#feat=promises
+
+	if ( !window.Promise ) {
+		throw 'The Promise class is not available natively. CKEditor is not compatible with this browser.';
+	}
+
+	return window.Promise;
+} );

+ 29 - 0
packages/ckeditor5-ui/tests/promise/promise.js

@@ -0,0 +1,29 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* globals describe, it, expect, CKEDITOR, window */
+
+'use strict';
+
+describe( 'Promise', function() {
+	it( 'should resolve properly', function( done ) {
+		CKEDITOR.require( [ 'promise' ], function( Promise ) {
+			var promise = new Promise( function( resolve ) {
+				// Fake an asynchronous operation.
+				window.setTimeout( function() {
+					resolve( 1 );
+				}, 0 );
+			} );
+
+			promise.then( function( value ) {
+				// then() catches errors, so we need to delay the execution.
+				window.setTimeout( function() {
+					expect( value ).to.equal( 1 );
+					done();
+				} );
+			} );
+		} );
+	} );
+} );