async function getData(params) {
try {
return sendRequest(params);
} catch (e) {
if (/** если ошибка не сетевая, то попробовать еще раз **/) {
return getData(params);
}
}
}
в констукторе класса можно сделать запрос в другое место?
Собственно для этого я и создвал метод create, чтобы в нем получить некоторые данные, затем уже манипулировать готовым блоком с полученным цветом и заданным руками типом.
Поскольку из конструктора нельзя вернуть промис (или можно?), пришлось добавить метод create.
мб можно даже просто обновлять color вызывая create при каждом интервале, хотя наверно лучше создавать новый объект
async function getRandomColorName() {
const response = await fetch('http://www.colr.org/json/color/random').then(r => r.json());
return response.colors[0].tags[0].name;
}
async function send(data) {
const response = await fetch('https://reqres.in/api/users', {
method: 'post',
mode: 'cors',
body: JSON.stringify(data),
})
.then(r => r.json())
.catch(console.log);
console.log( 'Data sent successfully, response: ', response );
}
function initSending(data, Construct) {
setInterval(async () => {
send({ type: 'brick', color: await getRandomColorName() });
}, 10000);
}
initSending();
class Block {
constructor(data) {
if (!data.type) {
throw new Error('No type passed!');
}
this.color = data.color || '';
this.type = data.type;
}
prepareForSending() {
this.color = this.color.toUpperCase();
return this;
}
}
async function getRandomColorName() {
const response = await fetch('http://www.colr.org/json/color/random').then(r => r.json());
return response.colors[0].tags[0].name;
}
async function send(data) {
const response = await fetch('https://reqres.in/api/users', {
method: 'post',
mode: 'cors',
body: JSON.stringify(data),
})
.then(r => r.json())
.catch(console.log);
console.log( 'Data sent successfully, response: ', response );
}
function initSending(data, Construct) {
setInterval(async () => {
const block = new Block({ type: 'brick', color: await getRandomColorName() });
send(block.prepareForSending());
}, 10000);
}
initSending();