This file contains 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
// This implementation does not work with Symbols, BigInts | |
function stringify(data) { | |
if (data === undefined) | |
return undefined | |
if (data === null) | |
return 'null' | |
if (data.toString() === "NaN") | |
return 'null' | |
if (data === Infinity) |
This file contains 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 PromisePolyfill(executor) { | |
let onResolve, onReject; | |
let fulfilled = false, | |
rejected = false, | |
called = false, | |
value; | |
function resolve(v) { | |
fulfilled = true; | |
value = v; |
This file contains 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
// ------------------------------------------------------------------- | |
// this function simulates a request that needs to run async | |
// ------------------------------------------------------------------- | |
function favoriteBlogger(favorite) { | |
return new Promise((resolve, reject) => { | |
if (favorite === 'rajatexplains') { | |
resolve(favorite); | |
} else { | |
reject('your preference is poor😛'); |
This file contains 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
// ------------------------------------------------------------------- | |
// this function simulates a request that needs to run async | |
// ------------------------------------------------------------------- | |
function doOtherAsyncThing() { | |
return new Promise((resolve) => { | |
setTimeout(() => resolve('it’s done!'), 500); | |
}); | |
} |
This file contains 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 getData() { | |
// using await means the resolved value of the Promise is returned! | |
const response = await fetch('https://blogger.ceo/api/blogs/image/random').then( | |
(response) => response.json(), | |
); // .then still works when it makes sense! | |
const otherResponse = await doOtherAsyncThing(); | |
// do stuff with `response` and `otherResponse` |
OlderNewer