Skip to content

Instantly share code, notes, and snippets.

View michaelwclark's full-sized avatar

Michael W Clark michaelwclark

View GitHub Profile
@michaelwclark
michaelwclark / Async Await Retry (Failure).js
Last active August 2, 2018 20:20
Examples of Async await retry
const ac = promise => promise.then(data => [null, data]).catch(err => [err])
const timeout = ms => new Promise(resolve => setTimeout(resolve, ms*1000))
const acFailRetry = asyncFn => async (...params) => {
const retryBlock = async (retry=0, errs=[]) => {
if(retry > 3)
throw errs
const [err, resp] = await ac(asyncFn(...params))
if(err) {
@michaelwclark
michaelwclark / sf.js
Created August 1, 2018 17:54
SF Test example
const sfModule = {
doInit: function(component, event,helper)
{
helper.getAvailable(component);
helper.getSelected(component);
},
}
export default sfModule
function isSolved(board) {
const getWins = makeGetWins(board)
return getWins(1) ? 1 : getWins(2) ? 2 : emptySlots(board) ? -1 : 0
}
const makeGetWins = board => key =>
[
horizontalWin(board, key),
verticalWin(board, key),
leftDiagWin(board, key),
@michaelwclark
michaelwclark / AsyncDestructureBlog.md
Last active April 15, 2018 04:55
Async/Await Destructuring

;TLDR: Don't write dependent promise chains; write destructured async/await using the following snippit.  GIST

const ad = async promise => {
  const [err, resp] = await promise.then(x => [{}, x]).catch(x => [x, {}])
  const [{ message }, { body }] = [err, resp]
  return [message, body]
}
@michaelwclark
michaelwclark / objIter.js
Created April 10, 2018 16:47
Javascript Object Iteration
const myObj = {
a:1,
b:2,
c:3
}
Object.keys(myObj).map((key)=> console.log({key:key,value: myObj[key]}))
Object.entries(myObj).map(kv=>console.log({key:kv[0], val: kv[1]}))
Object.values(myObj).map((v,idx)=>console.log({key:'unknown', value:v}))
@michaelwclark
michaelwclark / asyncDestructureTLDR.js
Last active April 9, 2018 18:29
TLDR for async await destructure article.
const ad = async promise => {
const [err, resp] = await promise.then(x => [{}, x]).catch(x => [x, {}])
const [{ message }, { body }] = [err, resp]
return [message, body]
}
const throwOrReturn = (err, val) => err ? throw err : return val
async function createUserA(){
const [err, body] = await ad(mockAPICallPromise({ body:{id: 1 }}))
@michaelwclark
michaelwclark / asyncDestructure2.js
Created April 9, 2018 03:36
Async Destructuring after refactor
// This helper method will allow us to capture succes and error
const ad = async promise => {
const [err, resp] = await promise.then(x => [{}, x]).catch(x => [x, {}])
const [{ message }, { body }] = [err, resp]
return [message, body]
}
// Simply returns what you pass in as promise form. Will reject if 2nd param
@michaelwclark
michaelwclark / asyncDestructure1.js
Created April 9, 2018 03:17
Async with destructuring v1
// Simply returns what you pass in as promise form. Will reject if 2nd param
// isn't null. Will resolve valued passed in otherwise. This is only for demonstration.
const mockAPICallPromise = (retVal, error = null) =>
new Promise((resolve, reject) => (error && reject(error)) || resolve(retVal))
async function createAndPopulateUserObject (successCallback, errorCallback) {
// Create user on API A on error callback error.
const [A_createUserErr, A_createUserResp] = await ad(mockAPICallPromise({ body:{id: 1 }}))
@michaelwclark
michaelwclark / asyncExample.js
Last active April 8, 2018 22:50
Async Without Destructuring
// Simply returns what you pass in as promise form. Will reject if 2nd param
// isn't null. Will resolve valued passed in otherwise. This is only for demonstration.
const mockAPICallPromise = (retVal, error = null) =>
new Promise((resolve, reject) => (error && reject(error)) || resolve(retVal))
async function createAndPopulateUserObject (successCallback, errorCallback) {
// Create user on API A on error callback error.
@michaelwclark
michaelwclark / PromiseAsyncCallback.js
Last active April 8, 2018 22:34
Promise + Callback Example
// Our example function to create and populate a user on 2 apis.
function createAndPopulateUserObject (successCallback, errorCallback) {
const A_createUser = mockAPICallPromise({ body: { id: 1 } })
// Functional programing no no, shouldn't need to update outer scope,
// but not a lot of options here.
let A_userID
let B_userID