Skip to content

Instantly share code, notes, and snippets.

View Gaafar's full-sized avatar

Gafi (Mostafa Gaafar) Gaafar

View GitHub Profile
@Gaafar
Gaafar / curryExample.js
Last active January 7, 2018 23:24
ES6 Currying Example
function add (x, y) {return x+y}
let curry = (f, ...args) => f.bind.apply(f,[null, ...args])
let add4 = curry(add,4)
console.log(add4(5)) //9
let add4n6 = curry(add4,6)
console.log(add4n6()) //10
@Gaafar
Gaafar / index.js
Created November 20, 2016 08:39
Get Github repos with contributors
const bb = require('bluebird');
const axios = require('axios');
const f = require('lodash/fp');
const autheader = 'token xxx';
const fetch = (page, continuePredicate, accumulator = [] ) =>
axios.get('https://api.github.com/orgs/:org/repos', {
params: {
page,
per_page: 100
@Gaafar
Gaafar / linux-server.sh
Last active May 20, 2017 21:23
machine setup
# install zsh
sudo apt-get update
sudo apt-get install zsh git -y
sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
chsh -s $(which zsh)
# manual: edit theme in ~/.zshrc to "clean"
# ssh-key
ssh-keygen -t rsa
@Gaafar
Gaafar / syntax-promise.js
Last active June 18, 2019 16:26
syntax-promise
const makeRequest = () =>
getJSON()
.then(data => {
console.log(data)
return "done"
})
makeRequest()
@Gaafar
Gaafar / syntax-async.js
Last active June 18, 2019 16:27
syntax-async
const makeRequest = async () => {
console.log(await getJSON())
return "done"
}
makeRequest()
const makeRequest = () => {
try {
getJSON()
.then(result => {
// this parse may fail
const data = JSON.parse(result)
console.log(data)
})
// uncomment this block to handle asynchronous errors
// .catch((err) => {
const makeRequest = async () => {
try {
// this parse may fail
const data = JSON.parse(await getJSON())
console.log(data)
} catch (err) {
console.log(err)
}
}
const makeRequest = () => {
return getJSON()
.then(data => {
if (data.needsAnotherRequest) {
return makeAnotherRequest(data)
.then(moreData => {
console.log(moreData)
return moreData
})
} else {
const makeRequest = async () => {
const data = await getJSON()
if (data.needsAnotherRequest) {
const moreData = await makeAnotherRequest(data)
console.log(moreData)
return moreData
} else {
console.log(data)
return data
}
const makeRequest = () => {
return promise1()
.then(value1 => {
// do something
return promise2(value1)
.then(value2 => {
// do something
return promise3(value1, value2)
})
})