Skip to content

Instantly share code, notes, and snippets.

View pfftdammitchris's full-sized avatar
💭
Dreaming

Christopher Tran pfftdammitchris

💭
Dreaming
View GitHub Profile
let state = { backgroundColor: 'white', profiles: [] }
let subscribers = []
function notifySubscribers(...args) {
subscribers.forEach((fn) => fn(...args))
}
function setBackgroundColor(color) {
setState((prevState) => ({
...prevState,
function notifySubscribers(...args) {
subscribers.forEach((fn) => fn(...args))
}
function setBackgroundColor(color) {
setState((prevState) => ({
...prevState,
backgroundColor: color,
}))
}
async function start() {
delay(100).then(() => start())
// This invocation's resolution is here
}
function map(arr, callback) {
const results = []
for (let index = 0; index < arr.length; index++) {
const item = arr[index]
// The promise ends up here. But this time we save the result inside our final collection that is being returned after the loop is finished
const result = callback(item)
results.push(result)
}
function forEach(arr, callback) {
for (let index = 0; index < arr.length; index++) {
const item = arr[index]
// The promise ends up here. But nothing else happens
callback(item)
}
}
async function start() {
while (true) {
await delay(200)
}
}
function wrapper() {
return new Promise((resolve, reject) => {
;(function innerWrapper() {
start()
.then(() => innerWrapper())
.catch((error) =>
reject(error instanceof Error ? error : new Error(String(error))),
)
})()
})
async function start() {
return delay(100).then(() => start())
}
async function start() {
try {
return 3
} catch (error) {
window.alert(`You picked out number. Please try again for a string`)
}
}