document.readyState !== 'complete'
. Однако если подгрузка чего-то происходит позднее, то, увы, об этом никак (надёжно) не узнать, потому что это поведение - чисто интерфейсное решение браузеров и нигде не документировано.useEffect(() => {
const fetchData = async (url: string, data: any[] = []) => {
const res = await fetch(url);
const json = await res.json();
if(json.pagination.links.next){
await fetchData(json.pagination.links.next, data)
}
data.push(json.data);
return data;
};
fetchData("https://gorest.co.in/public/v1/users").then(setData);
}, [url]);
useEffect(() => {
const fetchData = async (url: string) => {
let data: any[] = [];
do {
const res = await fetch(url);
const json = await res.json();
data.push(json.data);
url = json.pagination.links.next;
} while (url);
return data;
};
fetchData("https://gorest.co.in/public/v1/users").then(setData);
}, [url]);
any
- расставить твои типы надо. i
выходит за пределы массива, тебе надо было написать либо так:function funk (arr) {
for(let i = 0, length = arr.length - 1; i < length; i++) {
if(arr[i] === arr[i+1]) {
return true;
}
}
return false;
}
function funk (arr) {
for(let i = 1; i < arr.length; i++) {
if(arr[i - 1] === arr[i]) {
return true;
}
}
return false;
}
==
) из-за его ненадёжности, только полноценное сравнение - ===
.костыль
на ровном месте, решая ту самую "проблему", что имеет место быть в твоей реализации, вместо того чтоб подумать и написать один из предложенных мной выше вариантов.)костыль
и как его применение выглядит в коде.) created() {
if(mobile){
this.touchStart = () => {}
}
}
let touchStart;
if(mobile){
touchStart = function() {}
} else {
touchStart = function() {}
}
export {
touchStart
}
import {touchStart, ...} from './helpers';
methods: {
touchStart,
...
}
const touchMixin = {
methods: mobile ? {
touchStart() {}
} : {
touchStart() {}
}
}
mixins: [touchMixin],
calcAmount
по SOLID должен быть методом у объекта с этими ключами.const data = [{"id": 1, "a": 99, "b": 100}, {"id": 2, "a": 1 "b": 2}];
interface Calculable {
calcAmount(): number;
}
class CalculableAB implements Calculable {
constructor(obj) {
Object.assign(this, obj);
}
calcAmount() {
const {a, b} = this;
return a + b;
}
}
struct = data.map(obj => new CalculableAB(obj));
Calculable
ты просто вызываешь у него calcAmount()
. Как оно там реализовано - тебя не волнует.class AsyncQueue {
constructor(result) {
this.queue = [];
this.result = result;
}
isStreaming = false;
add(promise) {
this.queue.push(promise);
if(!this.isStreaming)
this.stream();
}
async stream() {
this.isStreaming = true;
for await (const result of this.queue) {
this.result(result);
}
this.queue.length = 0;
this.isStreaming = false;
}
}
data() {
const schedulesQueue = new AsyncQueue(this.showLoadedSchedules);
return {
schedulesQueue,
// ...
}
},
methods: {
showLoadedSchedules(response) {
if(this.curDateEvent) {
this.changeMobileDay(this.curDateEvent);
}
// ...
},
addSchedulesToQueue(locationId, gameModeId, selectedHeadsets, day) {
let param = 'locationId=' + locationId + '&gameModeId=' + gameModeId + '&headsets=' + selectedHeadsets
if (day)
param += '&day=' + day;
return this.schedulesQueue.add(
axios({
method: 'get',
url: 'schedules?' + param
}).catch((error) => {})
);
}
}