17 lines
386 B
JavaScript
17 lines
386 B
JavaScript
export default function fetchWithTimeout(ms, promise) {
|
|
return new Promise((resolve, reject) => {
|
|
const timeoutId = setTimeout(() => {
|
|
reject(new Error('promise timeout'));
|
|
}, ms);
|
|
promise.then(
|
|
res => {
|
|
clearTimeout(timeoutId);
|
|
resolve(res);
|
|
},
|
|
err => {
|
|
clearTimeout(timeoutId);
|
|
reject(err);
|
|
}
|
|
);
|
|
});
|
|
}
|