JSFiddle - React, Tailwind, and code Playground
by Ksenia Polyakova
JavaScript
const urls = [
'https://jsonplaceholder.typicode.com/photos',
'https://jsonplaceholder.typicode.com/posts/2',
'https://jsonplaceholder.typicode.com/users',
];
const ok = (resp, add) => console.log('выполнился без ошибок', resp, add);
const no = err => console.log('возникли ошибки', err);
class SerialRequest {
constructor() {
this.prevRes = null;
this.promise = Promise.resolve();
}
get(url, onRes, onRej) {
this.promise = this.promise
.then(() => fetch(url)
.then(res => {
onRes(res);
})
.catch(err => onRej(err)))
.catch(err => onRej(err));
return this;
}
}
const se = new SerialRequest();
se.get(urls[0], ok, no)
.get(urls[1], ok, no)
.get(urls[1], ok, no);