已关闭,该问题为opinion-based,目前不接受回答。
**想改进这个问题吗?**更新问题,以便editing this post可以用事实和引用来回答。
7天前关闭
Improve this question
我这里的用例:
- 如果资源立即可用,则返回它
- 如果一个资源还不可用,那么显示一个模态或加载指示器,然后等待它准备好
- 模态逻辑必须与promise/request逻辑解耦
有更好的设计模式吗?
let twoStagePromise = (state: boolean): Promise<{ ready: boolean, wait: Promise<any>; }> => {
return new Promise((resolve, reject) => {
try {
if (state) {
resolve({
ready: true,
wait: new Promise((resolve, reject) => {
resolve([1, 2, 3]);
})
});
} else {
resolve({
ready: false,
wait: new Promise((resolve, reject) => {
setTimeout(() => { resolve([1, 2, 3]); }, 1000);
})
});
}
} catch (e) {
reject(e);
}
});
};
twoStagePromise(false).then((data) => {
if (!data.ready) {
console.log('loading...');
}
return data.wait;
}).then((result) => {
console.log("recieved result", result);
}).catch((error) => {
console.log("try-catch failed or inner promise failed")
});
在阅读了一些注解后,我简化了代码:
let twoStagePromise = (state: boolean): Promise<number[] | Promise<number[]>> => {
return new Promise((resolve, reject) => {
try {
if (state) {
resolve([1, 2, 3]);
} else {
resolve(new Promise((resolve) => setTimeout(() => { resolve([1,2,3]) }, 1000)))
}
} catch (e) {
reject(e);
}
});
};
twoStagePromise(false).then((dataOrPromise) => {
if (dataOrPromise instanceof Promise) {
console.log('loading...');
}
return dataOrPromise // continue the promise chain by returning the promise or data
}).then((result) => {
console.log("recieved result", result);
}).catch((error) => {
console.log("try-catch failed or inner promise failed", error)
});
1条答案
按热度按时间lb3vh1jj1#
你可以创建一个函数,它可以显式地返回值或它的promise。在任何情况下,你都可以很容易地
await
它。要检查结果是否同步,使用result instanceof Promise