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
let state = { backgroundColor: 'white', profiles: [] } | |
let subscribers = [] | |
function notifySubscribers(...args) { | |
subscribers.forEach((fn) => fn(...args)) | |
} | |
function setBackgroundColor(color) { | |
setState((prevState) => ({ | |
...prevState, |
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 notifySubscribers(...args) { | |
subscribers.forEach((fn) => fn(...args)) | |
} | |
function setBackgroundColor(color) { | |
setState((prevState) => ({ | |
...prevState, | |
backgroundColor: color, | |
})) | |
} |
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() { | |
delay(100).then(() => start()) | |
// This invocation's resolution is here | |
} |
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
["1", 2, 3, "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
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 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) | |
} | |
} |
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() { | |
while (true) { | |
await delay(200) | |
} | |
} |
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 wrapper() { | |
return new Promise((resolve, reject) => { | |
;(function innerWrapper() { | |
start() | |
.then(() => innerWrapper()) | |
.catch((error) => | |
reject(error instanceof Error ? error : new Error(String(error))), | |
) | |
})() | |
}) |
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() { | |
return delay(100).then(() => start()) | |
} |
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 3 | |
} catch (error) { | |
window.alert(`You picked out number. Please try again for a string`) | |
} | |
} |