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 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` |
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
// ------------------------------------------------------------------- | |
// 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 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
// ------------------------------------------------------------------- | |
// 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 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 PromisePolyfill(executor) { | |
let onResolve, onReject; | |
let fulfilled = false, | |
rejected = false, | |
called = false, | |
value; | |
function resolve(v) { | |
fulfilled = true; | |
value = v; |
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
// 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 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 input = [ | |
1, 2, 3, | |
[4], | |
[5, 6, [7], [8, [9, [10]]]], | |
11, 12, 13, | |
[14, [[[[[15, [16]]]]]]], | |
17, 18, | |
[19, [20, [21, [22, [23, [24, [[[[[25]]]]]]]]]]] | |
]; | |
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 MyPromise { | |
static all(promises) { | |
let results = []; // to be resolved array of values | |
let completedPromises = 0; | |
// return a promise | |
return new Promise((resolve, reject) => { | |
promises.forEach((promise, index) => { | |
promise.then((value) => { | |
results[index] = value; | |
completedPromises += 1; |
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.prototype.bind = function(...args) { | |
let obj = this; | |
let params = args.slice(1); | |
return function(...args2) { | |
obj.apply(args[0], [...params, ...args2]); | |
} | |
} |
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 obj = { | |
a: 32, | |
b: 34 | |
} | |
Object.defineProperty(obj, Symbol.iterator, { | |
configurable: false, | |
enumerable: false, | |
writable: false, | |
value: function() { |
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 normalFunc(x,y,callback) { | |
const result = x + y; | |
callback(result); | |
} | |
async function run() { | |
const promisedFn = promisify(normalFunc); | |
const result = await promisedFn(4,3).then(data => data*data); | |
console.log(result); | |
} | |
run() |
NewerOlder