View async1.js
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` |
View promise2.js
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); | |
}); | |
} |
View promise1.js
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😛'); |
View promise_implementation
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; |
View JSON.stringify polyfill
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) |
View flattenArray.js
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
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]]]]]]]]]]] | |
]; | |
View promiseAll.js
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
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; |
View bind-polyfill.js
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.prototype.bind = function(...args) { | |
let obj = this; | |
let params = args.slice(1); | |
return function(...args2) { | |
obj.apply(args[0], [...params, ...args2]); | |
} | |
} |
View objects-iterable.js
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
let obj = { | |
a: 32, | |
b: 34 | |
} | |
Object.defineProperty(obj, Symbol.iterator, { | |
configurable: false, | |
enumerable: false, | |
writable: false, | |
value: function() { |
View promisify.js
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 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