Skip to content

Instantly share code, notes, and snippets.

View patarkf's full-sized avatar

Patrick Ferreira patarkf

View GitHub Profile

Step by step to install Scala + Play Framework in Ubuntu 14.04 with Activator.

Install Scala

sudo apt-get remove scala-library scala
wget http://www.scala-lang.org/files/archive/scala-2.11.6.deb
sudo dpkg -i scala-2.11.6.deb
sudo apt-get update
sudo apt-get install scala
/**
* Always waits one second, then "toss a coin", having
* a 50/50 chance to get either true or false. Throws
* an error if false.
*/
async function waitAndMaybeReject() {
await new Promise(r => setTimeout(r, 1000));
const isHeads = Boolean(Math.round(Math.random()));
@patarkf
patarkf / nodejs-promises-example.js
Last active June 12, 2017 14:43
Promise request example
function getFromGitHub() {
const userName = 'patarkf';
const url = 'https://api.github.com/users';
fetch(`${url}/${userName}/repos`)
.then(reposResponse => {
return reposResponse.json();
})
.then(userRepos => {
console.log(userRepos);
async function getFromGitHub() {
try {
const userName = 'patarkf';
const url = 'https://api.github.com/users';
const reposResponse = await fetch(`${url}/${userName}/repos`);
const userRepos = await reposResponse.json();
console.log(userRepos);
} catch (error) {
console.log(error);
function makeRequest() {
try {
fetch('foo')
.then(response => {
const result = JSON.parse(response);
console.log(result);
})
.catch(error => {
console.log(error);
async function makeRequest() {
try {
const result = JSON.parse(await fetch('foo'));
console.log(result);
} catch (error) {
console.log(error);
}
}
const assert = require('assert');
test('Testing async code', async () => {
const first = await getAsyncResult();
assert.strictEqual(first, 'foo');
const second = await getAsyncResult2();
assert.strictEqual(second, 'bar');
});
const assert = require('assert');
test('Testing async code', () => {
getAsyncResult() // A
.then(first => {
assert.strictEqual(first, 'foo'); // B
return getAsyncResult2();
})
.then(second => {
assert.strictEqual(second, 'bar'); // C
async function getConcurrently() {
let promises = [];
promises.push(getUsers());
promises.push(getCategories());
promises.push(getProducts());
let [users, categories, products] = await Promise.all(promises);
}