简体   繁体   English

如何编写异步函数以在 javascript 中查找字符串枚举?

[英]How to write async function to find string enumerations in javascript?

I did a function to find string enumerations but i didn `t succeed to repeat it in a async way, i'm not getting any result:我做了一个函数来查找字符串枚举,但我没有成功以异步方式重复它,我没有得到任何结果:

const stringEnumerationsAsync = async (str) => {
    let strChars = str.split("");
    let enumerations = new Set();

    const getEnumerations = (chars, enumertion) => {
        return new Promise((resolve) => {
            if (chars.length === 0) {
                resolve(enumertion);
            } else {
                for (let i = 0; i < chars.length; i++) {
                    let newChars = [...chars];
                    let char = newChars.splice(i, 1);
                    let newEnumeration = enumertion + char;
                    getEnumerations(newChars, newEnumeration).then((finalEnumeration) => {
                        enumerations.add(finalEnumeration);
                    });
                }
            }
        });
    };

    await getEnumerations(strChars, "");

    return [...enumerations];
};

There are a few issues:有几个问题:

  • with getEnumerations(newChars, newEnumeration).then((finalEnumeration)... you seem to expect one enumeration from the recursive call, but you should expect one or more enumerations.使用getEnumerations(newChars, newEnumeration).then((finalEnumeration)...您似乎期望从递归调用中获得一个枚举,但您应该期望一个或多个枚举。

  • In fact, you are mixing up two patterns: one where you collect the result from (resolved) return values, and one where you collect them in a non-local variable.实际上,您正在混合两种模式:一种是从(已解​​析的)返回值中收集结果,另一种是将它们收集到非局部变量中。 So if you go for the latter, you should not expect anything useful from the resolved value you get from the recursive call.因此,如果您选择后者,您不应该期望从递归调用中获得的解析值中得到任何有用的信息。 Instead add enumerations to the non-local variable in the base case, and only there.而是在基本情况下向非局部变量添加枚举,并且仅在那里。

  • In the else block you don't call resolve , meaning that the new Promise never gets resolved.else块中,您不会调用resolve ,这意味着new Promise永远不会得到解决。

  • You should actually not need to create a new Promise in the else case, as you will get promises from the recursive calls, and it is an anti-pattern to wrap that inside a promise constructor call back.else情况下,您实际上不需要创建new Promise ,因为您将从递归调用中获得new Promise ,并且将其包装在new Promise构造函数回调中是一种反模式。 So the if...else should be moved outside of new Promise所以if...else应该移到new Promise之外

  • And then in the if case you would get an immediately resolving promise, because you call resolve immediately.然后在if情况下,你会得到一个立即解决的承诺,因为你立即调用了resolve In that case you might as well call Promise.resolve instead of new Promise .在这种情况下,您不妨调用Promise.resolve而不是new Promise

  • Even more simple is to make getEnumerations an async function so that you only need to return the resolution value, and since we don't care about the resolution value,... well, we just need to return ;-)更简单的是让getEnumerations成为一个async函数,这样你只需要返回分辨率值,而且由于我们不关心分辨率值,......好吧,我们只需要返回 ;-)

So that brings us to this code:所以这把我们带到了这段代码:

 const stringEnumerationsAsync = async (str) => { let strChars = str.split(""); let enumerations = new Set(); const getEnumerations = async (chars, enumertion) => { if (chars.length === 0) { enumerations.add(enumertion); } else { for (let i = 0; i < chars.length; i++) { let newChars = [...chars]; let char = newChars.splice(i, 1); let newEnumeration = enumertion + char; await getEnumerations(newChars, newEnumeration); } } }; await getEnumerations(strChars, ""); return [...enumerations]; }; // demo stringEnumerationsAsync("abc").then(console.log); console.log("this is synchronous");


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

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