Skip to content

Instantly share code, notes, and snippets.

@tuor4eg
Last active June 28, 2018 12:38
Show Gist options
  • Save tuor4eg/ebca5a3fa4a6be48fdd6bf535914f2e5 to your computer and use it in GitHub Desktop.
Save tuor4eg/ebca5a3fa4a6be48fdd6bf535914f2e5 to your computer and use it in GitHub Desktop.
export default (backendUrl, currentUrl) => {
return new Promise((resolve, reject) => {
get(backendUrl)
.then(response => {
if (response.status !== 200) {
reject(new Error(response.status));
return;
}
const parseJs = JSON.parse(response.data);
Promise.all(parseJs.map(element => {
return get(`${element.url}/status`).then(response => JSON.parse(response.data));
}))
.then(response => response.sort((a, b) => a.workload > b.workload))
.then(response => post(currentUrl, {value: response[0].url}))
.then(response => {
if (response.status !== 201) {
reject(new Error(response.status));
return;
}
resolve(response);
});
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment