Async load inside replace function

后端 未结 2 1411
余生分开走
余生分开走 2021-01-06 01:49

I am working with replacements in Javascript. I did something like this:

var replacedText = originalText.replace(regex, function(value, i) { 
    return valu         


        
2条回答
  •  不思量自难忘°
    2021-01-06 02:25

    No, replace does only support synchronous callbacks. However, here is a generic function that accepts a callback which yields promises, and returns a promise for the string with all replacements made:

    function replaceAsync(str, re, callback) {
        // http://es5.github.io/#x15.5.4.11
        str = String(str);
        var parts = [],
            i = 0;
        if (Object.prototype.toString.call(re) == "[object RegExp]") {
            if (re.global)
                re.lastIndex = i;
            var m;
            while (m = re.exec(str)) {
                var args = m.concat([m.index, m.input]);
                parts.push(str.slice(i, m.index), callback.apply(null, args));
                i = re.lastIndex;
                if (!re.global)
                    break; // for non-global regexes only take the first match
                if (m[0].length == 0)
                    re.lastIndex++;
            }
        } else {
            re = String(re);
            i = str.indexOf(re);
            parts.push(str.slice(0, i), callback.apply(null, [re, i, str]));
            i += re.length;
        }
        parts.push(str.slice(i));
        return Promise.all(parts).then(function(strings) {
            return strings.join("");
        });
    }
    

提交回复
热议问题