Skip to content

Instantly share code, notes, and snippets.

View dsathyakumar's full-sized avatar
🎯
Focusing

DS dsathyakumar

🎯
Focusing
  • eBay
  • SLC
View GitHub Profile
@dsathyakumar
dsathyakumar / asyncToES6Promises.md
Last active April 26, 2016 12:27
Moving from async JS to ES6 promises

Moving (or) Converting from Async JS to ES6 Promises (Native)

The following summarises the learnings of migrating from the async promises module to ES6 promises, with a simple sample.

Async is a library containing a bunch of async utility functions like

  • async.parallel : To execute a bunch of async tasks in parallel.
  • async.waterfall: To execute a bunch of async tasks sequentially.

####The example had a combo of both:

<!DOCTYPE html>
<html>
<head>
<style>
body{
width: 960px;
margin: 0 auto;
padding: 0;
}
* {
// The Binary tree will be built with the Tree Node representation (Linked representation)
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}
// Convert a given array of data values into a binary tree representation
// This assumes a 0 index array
// So for an index i, the Left Child would be at 2(i) + 1
// The right Child will be at 2(i) + 2
// Post Order traversal => Left - Right - Data
// L-R-D
// Possible combinations
// 1. L-D
// Only Left exists. Right is absent. Since the Node has to be processed after the
// LEFT is processed, its pushed into the stack for later processing.
// Continue process the LEFT subtree.
// 2. R-D
// Only Right exists. Left is absent. Since the Node has to be processed after the
// RIGHT is processed, its pushed into the stack for later processing.
// A pre-order traversal means Data - LEFT - RIGHT
// So, the possible combinations are
// 1. D-L
// No Right => Process Node and proceed with Left SubTree
// No need to push into a stack for later processing (as there is no RIGHT)
// 2. D-R
// No Left => Process Node and proceed with Right SubTree.
// No need to push into a stack for later processing (as RIGHT exists)
// and RIGHT is being processed currently (due to absence of a LEFT)
// inOrder Traversal
// Left - Data - Right
// Note that the Data is processed only after LEFT is processed.
// Same way, Right is processed only after Left and Data are processed.
// So, the key here is, the moment a LEFT occurs, push it into the stack for later
// processing.
// So, when a Node is popped off the stack, its not guaranteed to have a RIGHT.
// So, when we hit a LEAF node, we pop the immediate previous ancestor, but,
// This is not guaranteed to have a RIGHT. Just process it.
@dsathyakumar
dsathyakumar / machine.js
Last active April 8, 2021 17:59
Generated by XState Viz: https://xstate.js.org/viz
const UploaderMachine = Machine(
{
strict: true,
id: "uploader-machine",
context: {
progress: false,
progressDataModel: {
progressMsg: "",
require('marko/node-require').install();
const http = require('http');
const server = require('http').createServer();
const port = 8080;
const indexTemplate = require('./index.marko');
server.on('request', (req, res) => {
indexTemplate.render({
name: 'Frank',
count: 30,
colors: ['red', 'green', 'blue']
{
"name": "marko3-http",
"version": "0.0.0",
"description": "Example of using Marko with a simple Node.js HTTP server",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"marko",
<html>
<head></head>
<body>
<p>Hello ${data.name}! You have ${data.count} messages!</p>
<ul if(data.colors && data.colors.length)>
<li style="color: ${color}" for(color in data.colors)>${color}</li>
</ul>
<include("./footer.marko") name=data.name/>
</body>
</html>