Easy access for a property in higher order functions, enables composition & piping
const strings = ["", "a"]
const filteredStrings = strings.filter(.length) // ["a"]
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 | |
}) |
# 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 |
#!/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 |
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() |
// 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 |