Skip to content

Instantly share code, notes, and snippets.

@hdorothea
Last active December 27, 2017 22:24
Show Gist options
  • Save hdorothea/ff86afd116a4f79839804ea66b8f4223 to your computer and use it in GitHub Desktop.
Save hdorothea/ff86afd116a4f79839804ea66b8f4223 to your computer and use it in GitHub Desktop.
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