如何在javascript/typescript中编写更简洁的2阶段promise?[关闭]

tkclm6bt  于 2023-04-22  发布在  TypeScript
关注(0)|答案(1)|浏览(125)

已关闭,该问题为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)
});
lb3vh1jj

lb3vh1jj1#

你可以创建一个函数,它可以显式地返回值或它的promise。在任何情况下,你都可以很容易地await它。要检查结果是否同步,使用result instanceof Promise

type Awaitable<T> = T | Promise<T>
function maybeImmediate<T>(
    call: () => Awaitable<T>,
    tryImmediate: boolean,
): Awaitable<T> {
    if (tryImmediate) {
        try {
            return call(); // you don't know if it's T or Promise<T> actually
        } catch (err) {
            // choose the one you want
            if (Math.random() > 0.5) {
                throw err;
            } else {
                return Promise.reject(err)
            }
        }
    } else {
        // make sure it's a promise
        return Promise.resolve().then(() => call())
    }
};

let cache: Record<string, string> = {};
function getCached(url: string): Awaitable<string> {
    if (cache[url]) return cache[url];
    return new Promise<string>(r => {
        setTimeout(() => r('result of ' + url), 1000)
    }).then(v => { cache[url] = v; return v })
}

相关问题