-
-
Notifications
You must be signed in to change notification settings - Fork 241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
封装一个工具函数输入一个promiseA返回一个promiseB如果超过1s没返回则抛出异常如果正常则输出正确的值。 #161
Comments
|
function timeoutPromise(promise, timeout) {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
reject(new Error(`Promise timed out after ${timeout} ms`));
}, timeout);
promise
.then((result) => {
clearTimeout(timer);
resolve(result);
})
.catch((error) => {
clearTimeout(timer);
reject(error);
});
});
} |
function timeout(promise, delay) { |
function getPromise(delay) { function fn(promise) { fn(getPromise(0)) |
|
function gongju(promiseA){
const promiseB = new Promise((resolve,reject) => {
promiseA.then(res => {
resolve(res)
})
setTimeout(() => {
reject(new Error('超时了'))
}, 1000);
})
return promiseB
}
const pA = new Promise((resolve,reject) => {
setTimeout(() => {
resolve('成功了')
}, 1500);
})
gongju(pA).then(res => {
console.log(res)
},err => {
console.log(err)
}) 不知道是不是这个意思 |
No description provided.
The text was updated successfully, but these errors were encountered: