Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                Open up the browser developer console (<i>not</i> the CodePen Console) to view output results.
              
            
!

CSS

              
                
              
            
!

JS

              
                /**
 * Data that similates a tree of folders with items in them.
 */
const data = {
	"items": [
		{
			"type": "folder",
			"title": "Folder 1",
			"items": [
				{
					"type": "folder",
					"title": "Folder 1.1",
					"items": [
						{
							"type": "link",
							"title": "Folder 1.1 | Item 1"
						},
						{
							"type": "link",
							"title": "Folder 1.1 | Item 2"
						},
						{
							"type": "link",
							"title": "Folder 1.1 | Item 3"
						},
						{
							"type": "folder",
							"title": "Folder 1.1.1",
							"items": [
								{
									"type": "link",
									"title": "Folder 1.1.1 | Item 1"
								},
								{
									"type": "link",
									"title": "Folder 1.1.1 | Item 2"
								}
							]
						}
					]
				},
				{
					"type": "folder",
					"title": "Folder 1.2",
					"items": [
						{
							"type": "folder",
							"title": "Folder 1.2.1",
							"items": [
								{
									"type": "link",
									"title": "Folder 1.2.1 | Item 1"
								},
								{
									"type": "link",
									"title": "Folder 1.2.1 | Item 2"
								},
								{
									"type": "link",
									"title": "Folder 1.2.1 | Item 3"
								}
							]
						},
						{
							"type": "link",
							"title": "Folder 1.2 | Item 1"
						},
						{
							"type": "link",
							"title": "Folder 1.2 | Item 2"
						},
						{
							"type": "link",
							"title": "Folder 1.2 | Item 3"
						},
						{
							"type": "folder",
							"title": "Folder 1.2.2",
							"items": [
								{
									"type": "link",
									"title": "Folder 1.2.2 | Item 1"
								},
								{
									"type": "link",
									"title": "Folder 1.2.2 | Item 2"
								}
							]
						},
						{
							"type": "folder",
							"title": "Folder 1.2.3",
							"items": [
								{
									"type": "link",
									"title": "Folder 1.2.3 | Item 1"
								},
								{
									"type": "link",
									"title": "Folder 1.2.3 | Item 2"
								},
								{
									"type": "link",
									"title": "Folder 1.2.3 | Item 3"
								},
								{
									"type": "folder",
									"title": "Folder 1.2.3.1",
									"items": [
										{
											"type": "link",
											"title": "Folder 1.2.3.1 | Item 1"
										},
										{
											"type": "link",
											"title": "Folder 1.2.3.1 | Item 2"
										}
									]
								},
								{
									"type": "link",
									"title": "Folder 1.2.3 | Item 4"
								}
							]
						},
						{
							"type": "link",
							"title": "Folder 1.2 | Item 4"
						},
						{
							"type": "link",
							"title": "Folder 1.2 | Item 5"
						}
					]
				}
			]
		},
		{
			"type": "folder",
			"title": "Folder 2",
		}
	]
};


/**
 * Counter for generating IDs.
 */
let idCounter = 0;


/**
 * Store the results so we can check at the end.
 */
let store = [];





const testyMcTestFace = {

	/**
	 * Entry point to run the tests.
	 */
	run() {

		this.main().then(() => {
			console.log("run(): DONE!");

			console.log("--- store ---");
			console.log(store);
		});
		
	},





	/**
	 * Start processing the tree of data here
	 */
	main() 
	{
		let promises = [];

		//-- Process first level of folders (i.e. the "trunk").
		for (var i = 0; i < data.items.length; i++) {
			var dataItem = data.items[i];

			promises.push(this.executeTrunkFolder(dataItem));
		}

		
		let promise = new Promise(function(resolve, reject) {
			//-- Exectute once all the trunk promises have been resolved (or rejected).
			//	 Calling resolve() or reject() here completes the process and tells the run()
			//	 function
			Promise.all(promises)
			.then((data) => {
				console.log("trunk(): All promises successfully completed.", data);
				resolve("trunk(): All promises successfully completed.", data);
			})
			.catch((e) => {
				console.log("trunk(): Reject caught.", e);
				reject("trunk(): Reject caught.", e);
			});

		});

		return promise;
	},



	/**
	 * Process a first level folder off the trunk.
	 */
	executeTrunkFolder(item)
	{
		const _this = this;

		let promise = new Promise(function(resolve, reject) {

			_this.asyncFunction(item, { ID: 0}).then((resultItem) => {
				//-- Process sub-folders/items of this folder.
	
				_this.processItems(item, resultItem).then(() => {
					//-- All sub-items have successfully processed
					resolve("executeTrunkFolder(): Items resolved. item=" + resultItem.ID + "|" + resultItem.type + "|" + resultItem.title);
				}).catch( () => {
					reject("reject(): Items resolution failed. item=" + resultItem.ID + "|" + resultItem.type + "|" + resultItem.title);
				});
	
			});

		});

		return promise;
	},



	/**
	 * Process all items in a folder. Add each item and sub-folder to the `store`.
	 */
	processItems(folder, parentFolder) 
	{
		const _this = this;

		let promises = [];

		let totalItems = 0;
		if (folder.items != undefined) totalItems = folder.items.length;

		console.log("--------------------");
		console.log("processItems(" + parentFolder.title + "): folder.title=" + folder.title + " | " + totalItems + " items.");


		if (totalItems == 0) 
		{
			// Nothing to process, so resolve immediately
			console.log("processItems(" + parentFolder.title + "): No children");
			
			let promise = new Promise(function (resolve, reject) {
				resolve("processItems(" + parentFolder.title + "): No children");
			});

			return promise;
		}
		else 
		{
			for (var i = 0; i < folder.items.length; i++) 
			{
				const dataItem = folder.items[i];

				if (dataItem.type == "folder") {
					//-- Process a sub-folder. 
					//	 Process it via a helper-function so `dataItem` us not changed before code in the helpers finishes execution.
					console.log("processItems(" + parentFolder.title + "): dataItem=" + dataItem.type + "|" + dataItem.title);

					promises.push(_this.executeFolder(dataItem, parentFolder));
				}
				else 
				{
					//-- Process an item in the folder.
					console.log("processItems(" + parentFolder.title + "): item=" + dataItem.type + "|" + dataItem.title);

					promises.push(_this.executeItem(dataItem, parentFolder));
				}
			}
		}


		let promise = new Promise(function (resolve, reject) {
			//-- Once all items have been processed in this folder, alert the the calling function.
			Promise.all(promises)
			.then(() => {
				console.log("processItems(" + parentFolder.title + "): All promises successfully completed.");
				resolve("processItems(" + parentFolder.title + "): All promises successfully completed.");
			})
			.catch((e) => {
				console.log("processItems(" + parentFolder.title + "): Reject caught.");
				reject("processItems(" + parentFolder.title + "): Reject caught.");
			});
		});

		return promise;
	},



	/**
	 * Process a `folder` type item. 
	 * Call an async function that adds the item to the store.
	 */
	executeFolder(item, parentFolder) 
	{
		const _this = this;

		console.log("executeFolder(): ", item, parentFolder);

		let promise = new Promise(function (resolve, reject) {

			_this.asyncFunction(item, parentFolder)
			.then((resultItem) => {
				_this.processItems(item, resultItem).then(() => {
					// Successfully performed the action
					console.log("executeFolder.then(" + parentFolder.title + "):", resultItem);
					resolve("executeFolder.then(" + parentFolder.title + "): New folder ID=" + resultItem.ID);
				});
			})
			.catch( (e) => {
				// Failed to perform the action.
				console.log("executeFolder.catch(" + parentFolder.title + "):", e);
				reject("executeFolder.cathch(" + parentFolder.title + "):");
			});

		});

		return promise;
	},



	/**
	 * Process an `item` type item. 
	 * Call an async function that adds the item to the store.
	 */
	executeItem(item, parentFolder)
	{
		const _this = this;

		console.log("executeItem(): ", item, parentFolder);

		let promise = new Promise(function (resolve, reject) {

			_this.asyncFunction(item, parentFolder)
			.then((resultItem) => {
				// Successfully performed the action
				console.log("executeItem.then(" + parentFolder.title + "):", resultItem);
				resolve("executeItem.then(" + parentFolder.title + "):");
			})
			.catch( (e) => {
				// Failed to perform the action
				console.log("executeItem.catch(" + parentFolder.title + "):", e);
				reject("executeItem.cathch(" + parentFolder.title + "): New item ID=" + resultItem.ID);
			});

		});

		return promise;
	},
	




	asyncFunction(item, parentFolder) {
		console.log("asyncFunction(): ", item, parentFolder);

		// Wait 500ms then add the supplied item to the store
		let promise = new Promise(function (resolve, reject) {
			setTimeout(() => {
				idCounter = idCounter + 1;

				item.ID = idCounter;

				store.push(
				{
					item: item,
					parent: parentFolder
				});

				resolve(item);
			}, 500);
		});

		return promise;
	}

}


testyMcTestFace.run();
              
            
!
999px

Console