Skip to content

Instantly share code, notes, and snippets.

View Gaafar's full-sized avatar

Gafi (Mostafa Gaafar) Gaafar

View GitHub Profile
const makeRequest = () => {
return promise1()
.then(value1 => {
// do something
return Promise.all([value1, promise2(value1)])
})
.then(([value1, value2]) => {
// do something
return promise3(value1, value2)
})
const makeRequest = async () => {
const value1 = await promise1()
const value2 = await promise2(value1)
return promise3(value1, value2)
}
const makeRequest = () => {
return callAPromise()
.then(() => callAPromise())
.then(() => callAPromise())
.then(() => callAPromise())
.then(() => callAPromise())
.then(() => {
throw new Error("oops");
})
}
const makeRequest = async () => {
await callAPromise()
await callAPromise()
await callAPromise()
await callAPromise()
await callAPromise()
throw new Error("oops");
}
makeRequest()
// this will not work in top level
// await makeRequest()
// this will work
makeRequest().then((result) => {
// do something
})
@Gaafar
Gaafar / dot-function.md
Last active July 24, 2017 04:49
dot-function

Motivation

Easy access for a property in higher order functions, enables composition & piping

Examples

const strings = ["", "a"]
const filteredStrings = strings.filter(.length) // ["a"]
@Gaafar
Gaafar / certbot-renew
Last active November 12, 2017 16:35
certbot renew cron
# cron tab
# 0 0 */5 * * /root/certbot-renew
#!/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
#old /root/certbot-auto renew
certbot renew
# don't forget chmod +x /root/certbot-renew
@Gaafar
Gaafar / git-shove.sh
Last active April 1, 2019 12:05
shove to branch
#!/bin/bash
# install:
# copy this file to /usr/local/bin/shove
# sudo chmod +x /usr/local/bin/shove
# then use: shove alpha
remoteBranch=${1?Please specify origin branch to shove}
localBranch=`git symbolic-ref --short -q HEAD`
read -p "Force push local branch ($localBranch) => (origin/$remoteBranch). Proceed? (y/n) " -r
@Gaafar
Gaafar / sync-error.js
Last active May 19, 2019 09:01
.catch with sync error
const getJSON = () => {
// here url is not defined and will throw an error
// since the error is thrown before returning the promise,
// it won't be caught by .catch
console.log(url)
return Promise.resolve()
}
const makeRequest = () => getJSON()
@Gaafar
Gaafar / promise-utils.js
Created May 19, 2019 09:27
promise-utils
// version 1: using promise with utility functions
const passthrough = fn => data => Promise.all([data, fn(data)])
const spread = fn => list => fn(...list)
const makeRequest = () => promise1()
.then(passthrough(promise2))
.then(spread(promise3))
// version 2: async/await