function timeoutPromise(wait, promise) {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
reject(new Error('fetch timeout'));
}, wait);
promise.then(
(res) => {
console.log(res)
clearTimeout(timer);
resolve(res);
},
(err) => {
clearTimeout(timer);
reject(err);
}
);
});
}
const fetchWithTimeout = timeoutPromise(10, fetch('https://jsonplaceholder.typicode.com/users'));
fetchWithTimeout.then(res => {
return res.json();
}).then(data => {
console.log(data);
}).catch(err => {
console.log(err)
})
console