This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Runtime.CompilerServices; | |
using System.Threading.Tasks; | |
// Example 4: await IEnumerable<Task<T>> | |
// Just a dummy async function | |
public static Task<string> GetSomethingAsync(int number) => Task.FromResult($"Something {number}"); | |
// This extension method allows us to await many tasks | |
public static TaskAwaiter<T[]> GetAwaiter<T>(this IEnumerable<Task<T>> manyTasks) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Runtime.CompilerServices; | |
using System.Threading.Tasks; | |
// Example 3: await Lazy<Task<T>> | |
// Just a dummy async function | |
public static Task<string> GetSomethingAsync(int number) => Task.FromResult($"Something {number}"); | |
// We need a lazy variable to work with | |
var lazySomething = new Lazy<Task<string>>(() => GetSomethingAsync(10)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Runtime.CompilerServices; | |
using System.Threading.Tasks; | |
// Example 2: await anything | |
public static TaskAwaiter<T> GetAwaiter<T>(this T nonAwaitable) | |
=> Task.FromResult(nonAwaitable).GetAwaiter(); | |
// Now we can write code like this | |
await "Hello world"; | |
await 100; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Runtime.CompilerServices; | |
using System.Threading.Tasks; | |
// Example 1: await strings | |
public static TaskAwaiter<string> GetAwaiter(this string text) | |
=> Task.FromResult(text).GetAwaiter(); | |
// Now it doesn't complain when we await the string | |
await "Hello world"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const result = pipe([1, 2, 3]) | |
.pipe(through) | |
.pipe(i => some('steps', i)) | |
.pipe(toAsync) | |
.pipe(delayed(100)) | |
.then(concat) | |
function* through(iterator){ | |
for(const item of iterator){ | |
yield* new Array(item).fill(item); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function run(){ | |
console.log('start'); | |
const result = await source() | |
::map(t => t*2) | |
::take(30) | |
::buffer(10) | |
::map(t => Promise.delay(Math.random()*10, t)) | |
::filter(t => t%4 == 0) | |
::reduce((a, b) => a+b, 0); | |
console.log('done', result); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function* source(){ | |
let n = 1; | |
while(true){ | |
yield n++; | |
await new Promise(res => setTimeout(res, 100)); | |
} | |
} | |
async function* map(func){ | |
for await(const chunk of this){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Given the following generator that will walk a tree structure depth-first | |
// for each subtree it will go deeper unless the consumer returns `false` | |
function* depthFirst(tree, prefix=''){ | |
for(const [key, value] of Object.entries(tree)){ | |
const path = `${prefix}/${key}`; | |
if(typeof value === 'string'){ | |
yield {path, value} | |
}else{ | |
if(false !== (yield {path})){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var component1 = { | |
//this method will receive the event and process it | |
handleEvent: function(event){ | |
//this is only for debugging | |
console.log("debugging!", event.data); | |
//the event causes the state of the component to change |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module _ from "underscore"; | |
import _ from "underscore"; | |
import module _ from "underscore"; | |
import {each, map, find} from "underscore"; | |
import "underscore"; | |
import default _ from "underscore"; | |
const _ = System.import("underscore"); | |
import "underscore" as _; | |
import {_} from "underscore"; | |
import {each as forEach} from "underscore"; |