内部异步加载替换功能

Gen*_*tto 4 javascript asynchronous replace callback

我正在使用Javascript中的替换.我做了这样的事情:

var replacedText = originalText.replace(regex, function(value, i) { 
    return value + 'some_additional_data';
});

return replacedText;
Run Code Online (Sandbox Code Playgroud)

但是现在我需要在replace方法中加载HTML模板.以这种方式调用load方法:

res.render(location, json, function(error, html) {
    //i have the html loaded with my json data
});
Run Code Online (Sandbox Code Playgroud)

我需要在我的replace方法中加载它,但我无法做到:

var replacedText = originalText.replace(media, function(value, i) {
    var json = buildJSON(value);
    res.render(location, json, function(error, html) {
        //how could i return the "html" object for the replace function?
    });
});
Run Code Online (Sandbox Code Playgroud)

我尝试过类似的东西,但它不起作用:

var replacedText = originalText.replace(media, function(value, i) {
    var json = buildJSON(value);
    return res.render(location, json, function(error, html) {
        return html;
    });
});
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激,非常感谢您提前

Ber*_*rgi 6

不,replace仅支持同步回调.但是,这是一个通用函数,它接受一个产生promise的回调,并返回一个包含所有替换的字符串的promise:

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("");
    });
}
Run Code Online (Sandbox Code Playgroud)


jfr*_*d00 5

当您有一个需要同步返回值的回调时,您不能在该回调内使用异步操作来获取该值。异步操作(根据定义)将在回调返回后的某个时间完成,因此异步操作的结果无法从回调返回,并且无法让 JS 等待异步操作。

我不完全理解你的代码想要做什么,但从你的话来看,听起来你想加载一个 HTML 模板并在替换操作中使用它。有一些不同的方法可以解决这个问题。

例如,您可以通过两次传递来完成此操作。

  1. 第一遍实际上并没有更改您的字符串,而是只是构建了所需模板的列表。

  2. 然后,加载该列表中的所有模板。

  3. 然后,当加载了您需要的所有模板后,您就可以进行替换,使用已加载的模板来执行您计划的同步替换。