Skip to content

Instantly share code, notes, and snippets.

View spion's full-sized avatar
:shipit:

Gorgi Kosev spion

:shipit:
View GitHub Profile
type Checker<T> = (x: any) => x is T;
function tArray<T>(f: Checker<T>) {
return function (a:any): a is Array<T> {
if (!Array.isArray(a)) return false;
for (var k = 0; k < a.length; ++k)
if (!f(a[k])) return false;
return true;
}
}
#!/bin/sh
export "$(grep -az '^DBUS_SESSION_BUS_ADDRESS=' /proc/`ps -e | grep 'i3$' | awk '{print $1}'`/environ)"
/bin/bash -c "$*"
exit 0
@spion
spion / modules.md
Last active February 12, 2017 19:49

Why .mjs

Alternative 1

As far as I can tell from the latest update, the ability to import ES6 import from CommonJS modules is off the table. The main argument for this was to enable existing modules to upgrade to ES6 without causing any breakage to their CommonJS dependants.

This is a big deal. This means that CommonJS libraries that want to upgrade to ES6 modules will HAVE to bump semver-major version to

function translateError(msg) {
var newErr = new Error(msg); // placed here to get correct stack
return e => {
newErr.originalError = e;
throw newErr;
}
}
async function asyncTask() {
const user = await UserModel.findById(1).catch(translateError('No user found'))
type ValidationError = string | boolean | null;
function fNoError<T>(t:T):ValidationError { return null }
class FieldState<TField> {
private validator: (val:TField) => ValidationError = fNoError
@observable focused = false
@observable changed = false
@observable finisedEditing = false;
type Json = null | boolean | string | number | Array<{}> | {}
function tArray<T>(f:(t:any) => t is T) {
return function (a:any): a is Array<T> {
if (!Array.isArray(a)) return false;
for (var k = 0; k < a.length; ++k)
if (!f(a[k])) return false;
return true;
}
}
import {render} from 'react-dom'
import {observable, computed, action} from 'mobx'
import {observer} from 'mobx-react'
import * as React from 'react'
import {fromPromise, IPromiseBasedObservable} from 'mobx-utils'
import 'whatwg-fetch'
import {computedAsync, matchAsync} from './computed-async'
function delay<T>(n:number) {
function getData() {
var respondent = Respondent.findById(respondentId)
var box = respondent.then(r => Box.findById(r.box_id))
var user = box.then(box => User.findById(box.owned_by))
return Promise.props({user, box, respondent})
}

Async-sync correspondence

Just like typical expressions are evaluated at time of assignment, promise expressions are also evaluated eagerly.

Promise values act like regular values. You can reuse regular variable values later:

var x = someExpression()

// some other method or function, later:
// These theoretical API requests are deferred promises;
// Not executed until `.then()` is invoked.
let a = new Request('/foo');
let b = new Request('/bar');
let c = new Request('/baz');
// If invoked directly, then issue 3 direct HTTP requests:
Promise.all([ a, b, b ]).then((results) => {
// GET /foo
// GET /bar