Skip to content

Instantly share code, notes, and snippets.

View pfftdammitchris's full-sized avatar
💭
Dreaming

Christopher Tran pfftdammitchris

💭
Dreaming
View GitHub Profile
async function start() {
try {
return await getRandomItem(['a', 1, 3, 'ccc'])
} catch (error) {
window.alert(`You picked out number. Please try again for a string`)
}
}
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 delay(ms, value) {
return new Promise((resolve) => {
setTimeout(() => resolve(value), ms)
})
}
async function getRandomItem(arr) {
try {
const result = await delay(200, arr[Math.floor(Math.random() * arr.length)])
async function start() {
const arr = ['1', 2, 3, 'hello'].map((item) => Promise.resolve(item))
const newArr = []
arr.forEach(async (item) => {
const result = await item
newArr.push(result)
})
console.log(newArr)
function LocalStorage() {
let _data = {}
let _options = {}
return {
get(key) {
return _data[key]
},
set(key, value) {
_data[key] = value
const MySingleton = {
sayHello() {
console.log('hello')
},
}
class LocalStorage {
#data = {}
constructor(options) {
this.options = options || {}
}
get(key) {
return this.#data[key]
}
class LocalStorage {
#data = {}
static _instance = null
static getInstance(options) {
if (!LocalStorage._instance) {
LocalStorage._instance = new LocalStorage(options)
}
const localStorage = LocalStorage()
function LocalStorage(options) {
let _data = {}
let _options = options || {}
return {
get(key) {
return _data[key]
},
set(key, value) {
_data[key] = value