Created
March 18, 2024 16:39
-
-
Save IAmAnubhavSaini/5a982daa3037d0d67365f49b5607bbd3 to your computer and use it in GitHub Desktop.
generatePairs and Future
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 Future { | |
#value = null; | |
#error = null; | |
#successCallbacks = []; | |
#failureCallback = () => {} | |
#finalCallback = () => {} | |
constructor(fn) { | |
const onSuccess = (value) => { | |
this.#value = value; | |
this.state = 'succeeded'; | |
this.#successCallbacks.forEach(cb => cb(this.#value)); | |
this.#finalCallback({state: this.state, value: this.#value, error: this.#error}); | |
} | |
const onFailure = (error) => { | |
this.#error = error; | |
this.state = 'failed'; | |
this.#failureCallback(this.#error); | |
this.#finalCallback({state: this.state, value: this.#value, error: this.#error}); | |
} | |
this.state = 'pending'; | |
this.timeout = 1; | |
fn(onSuccess, onFailure); | |
} | |
then(fnSuccess) { | |
if(this.state === 'pending') { | |
this.#successCallbacks.push(fnSuccess); | |
return this; | |
} | |
fnSuccess(this.#value); | |
return this; | |
} | |
catch(fnError) { | |
if(this.state === 'pending') { | |
this.#failureCallback = fnError; | |
return this; | |
} | |
fnError(this.#error); | |
return this; | |
} | |
finally(fnFinally) { | |
if(this.state === 'pending') { | |
this.#finalCallback = fnFinally; | |
return; | |
} | |
fnFinally(this.#value); | |
} | |
} | |
function doesSomethingAsync(a, b) { | |
return new Future(function fn(success, failure) { | |
setTimeout(() => { | |
if (b === 0) { | |
return failure("Cannot divide by 0"); | |
} | |
return success(a / b); | |
}, 2 * 1000); | |
}); | |
} | |
doesSomethingAsync(10, 20) | |
.then(console.log) // 0.5 | |
.then(value => console.info("info", value)) // info 0.5 | |
.catch(console.error) | |
.finally(console.log); // { state: 'succeeded', value: 0.5, error: null } | |
doesSomethingAsync(10, 0) | |
.then(console.log) // | |
.then(value => console.info("info", value)) // | |
.catch(console.error) // Cannot divide by 0 | |
.finally(console.log); // { state: 'failed', value: null, error: 'Cannot divide by 0' } | |
const x = doesSomethingAsync(22, 7); | |
console.log(x, typeof x); // Future { state: 'pending', timeout: 1 } object | |
setTimeout(() => {}, 3000); | |
x.then(console.log); // 3.142857142857143 |
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 generatePairs(n) { | |
if(n < 0) { | |
return []; | |
} | |
const pairs = []; | |
for(let diff = 1; diff <= n; diff++) { | |
for(let i = 0; i < n; i++) { | |
if(i + diff > n) { break; } | |
pairs.push([i, i+diff]); | |
} | |
} | |
return pairs; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment