Why is GetSeNameAsync async ?

6 месяцев назад
I got an import logic for a batch of 500 products. I want to fetch Slugs present in the db from the products batch based on the slug determined from the product name. When we create the slug this function is executed:

public virtual Task<string> GetSeNameAsync(string name, bool convertNonWesternChars, bool allowUnicodeCharsInUrls)
        {
            if (string.IsNullOrEmpty(name))
                return Task.FromResult(name);

            var okChars = "abcdefghijklmnopqrstuvwxyz1234567890 _-";
            name = name.Trim().ToLowerInvariant();

            if (convertNonWesternChars && _seoCharacterTable == null)
                InitializeSeoCharacterTable();

            var sb = new StringBuilder();
            foreach (var c in name.ToCharArray())
            {
                var c2 = c.ToString();
                if (convertNonWesternChars)
                {
                    if (_seoCharacterTable?.ContainsKey(c2) ?? false)
                        c2 = _seoCharacterTable[c2].ToLowerInvariant();
                }

                if (allowUnicodeCharsInUrls)
                {
                    if (char.IsLetterOrDigit(c) || okChars.Contains(c2))
                        sb.Append(c2);
                }
                else if (okChars.Contains(c2))
                {
                    sb.Append(c2);
                }
            }

            var name2 = sb.ToString();
            name2 = name2.Replace(" ", "-");
            while (name2.Contains("--"))
                name2 = name2.Replace("--", "-");
            while (name2.Contains("__"))
                name2 = name2.Replace("__", "_");

            return Task.FromResult(name2);
        }

I was wondering why is it async as we don't do asynchronous operations here ?
6 месяцев назад
Because the method is defined async in the interface. In the default implementation there are no asynchronous operations, but in others (customization or third-party plugins) it can be.
6 месяцев назад
Ok i see thank you!