Skip to content

Instantly share code, notes, and snippets.

let x = "1";
runAsync(() => {
x = "2";
// this will run second (or never)
console.log(x);
// this is pointless
return x;
});
// this will run first
let x = 0;
x = setTimeout(() => {
++x;
console.log(x);
return x;
}, 1000);
console.log(x);
import {get, request} from "http";
import {readFile, writeFile} from "fs";
let host = "localhost";
let port = 8917;
get({host, port}, response => {
let body = "";
response.on("readable", () => {
import {get, request} from "http";
import {readFile, writeFile} from "fs";
let host = "localhost";
let port = 8917;
let body = "";
get({host, port}, completeRequest(finalCb));
function finalCb(err, result) {
function runSync(cb) {
cb();
}
let x = 0;
runSync(() => ++x);
console.log(x);
let x = 0;
Array.of(1, 2, 3).forEach(() => ++x);
console.log(x);
function isItAsync(file, cb) {
if (!file) {
cb("no file");
}
fs.readFile(file, cb);
}
import {readFile} from "fs";
let file;
function isItAsync(file, cb) {
if (!file) {
return cb("no file");
}
readFile(file, cb);
}
import request from "request-promise";
import {readFile, writeFile} from "mz/fs";
let uri = "http://localhost:8917";
request.get({uri})
.then(body => writeFile(__dirname + "/foo", body))
.then(() => readFile(__dirname + "/foo"))
.then(contents => writeFile(__dirname + "/bar", contents.toString().split("").reverse().join("")))
.then(() => request({uri, method: "POST"}))
import request from "request-promise";
import {readFile, writeFile} from "mz/fs";
import co from "co";
let uri = "http://localhost:8917";
co(function* () {
let body = yield request.get({uri});
yield writeFile(__dirname + "/foo", body);
let contents = yield readFile(__dirname + "/foo");