Last active
December 27, 2017 22:24
-
-
Save hdorothea/ff86afd116a4f79839804ea66b8f4223 to your computer and use it in GitHub Desktop.
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
var FAILURE_COEFF = 10; | |
var MAX_SERVER_LATENCY = 200; | |
function getRandomBool(n) { | |
var maxRandomCoeff = 1000; | |
if (n > maxRandomCoeff) n = maxRandomCoeff; | |
return Math.floor(Math.random() * maxRandomCoeff) % n === 0; | |
} | |
function getSuggestions(text) { | |
var pre = 'pre'; | |
var post = 'post'; | |
var results = []; | |
if (getRandomBool(2)) { | |
results.push(pre + text); | |
} | |
if (getRandomBool(2)) { | |
results.push(text); | |
} | |
if (getRandomBool(2)) { | |
results.push(text + post); | |
} | |
if (getRandomBool(2)) { | |
results.push(pre + text + post); | |
} | |
return new Promise((resolve, reject) => { | |
var randomTimeout = Math.random() * MAX_SERVER_LATENCY; | |
setTimeout(() => { | |
if (getRandomBool(FAILURE_COEFF)) { | |
reject(); | |
} else { | |
resolve(results); | |
} | |
}, randomTimeout); | |
}); | |
} | |
console.log(getSuggestions('hallo').then(res => console.log(res))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment