简体   繁体   English

如何打破承诺链

[英]How to break chain in promise

I'm trying to limit the number of apis fetches in my project by saving them in a simple cache, key collection in mongodb. 我试图通过将它们保存在mongodb中的简单缓存,键集合中来限制我的项目中提取的api的数量。 Is thera way to stop propagation of .then() inside Promise, without using async/await? 是Thera的方法,可以在不使用async / await的情况下阻止.then()在Promise中的传播吗?

export const getData = (url: string) => {
    return new Promise((resolve, reject) => {
        findInCache(url)
            .then((cached: string | null) => {
                if (cached) {
                    resolve(cached);
                }
            })
            .then(() => {
                axios
                    .get(url)
                    .then(({data}) => {
                        setCache(url, data, TTL);
                        resolve(data);
                    })
                    .catch(e => reject(e));
            });
    });
};

Firstly, lets get rid of the Promise constructor anti-pattern - your function call inside the promise executor returns a promise, so, no need for anew Promise 首先,让我们摆脱Promise构造函数的反模式-在promise executor中的函数调用返回一个promise,因此,不需要新的Promise

Secondly, only run the second request if the result of the first is empty 其次,仅在第一个请求为空的情况下运行第二个请求

export const getData = (url) => findInCache(url)
// here we return haveResult and avoid axios.get(url) altogether
.then((haveResult) => haveResult || axios.get(url)
    // sometimes nested .then is useful like in this case
    .then(({data}) => {
        setCache(url, data, TTL);
        return data;
    })
);

When you return something result in then , this result is come into next then function. 当您在then返回结果时,该结果进入next then函数。 So, you can control what you would do in next then based on input parameter inCache . 所以,你可以控制你的下一步该怎么做then根据输入参数inCache So you can do something like: 因此,您可以执行以下操作:

export const getData = (url: string) => {
    return new Promise((resolve, reject) => {
        findInCache(url)
            .then((cached: string | null) => {
                if (cached) {
                    resolve(cached);
                    return true;
                }
                return false;
            })
            .then((inCache) => {
                if (!inCache) {
                  axios
                      .get(url)
                      .then(({data}) => {
                          setCache(url, data, TTL);
                          resolve(data);
                      })
                      .catch(e => reject(e));
               }
            });
    });
};

you can just do this instead instead of chaining. 您可以执行此操作,而不是链接。 if it is in cache then fetch from cache else get from url 如果它在缓存中,则从缓存中获取,否则从网址中获取

export const getData = (url: string) => {
    return new Promise((resolve, reject) => {
        findInCache(url)
            .then((cached: string | null) => {
                if (cached) {
                    resolve(cached);
                } else {
                  axios
                    .get(url)
                    .then(({data}) => {
                        setCache(url, data, TTL);
                        resolve(data);
                    })
                    .catch(e => reject(e));
                  }
            })

    });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM