This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function LocalStorage() { | |
let _data = {} | |
let _options = {} | |
return { | |
get(key) { | |
return _data[key] | |
}, | |
set(key, value) { | |
_data[key] = value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MySingleton = { | |
sayHello() { | |
console.log('hello') | |
}, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LocalStorage { | |
#data = {} | |
constructor(options) { | |
this.options = options || {} | |
} | |
get(key) { | |
return this.#data[key] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LocalStorage { | |
#data = {} | |
static _instance = null | |
static getInstance(options) { | |
if (!LocalStorage._instance) { | |
LocalStorage._instance = new LocalStorage(options) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const localStorage = LocalStorage() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function LocalStorage(options) { | |
let _data = {} | |
let _options = options || {} | |
return { | |
get(key) { | |
return _data[key] | |
}, | |
set(key, value) { | |
_data[key] = value |