Q-Javascript承诺等待数组被填充
我是Q库的新手,我对Q对数组方法的承诺有一些理解上的问题。 我有一个数组,其中包含各种键字符串,可以用延迟函数替换。 在成功替换传入的数组之后,我需要继续使用现在完成的数组。 在这种情况下,如何等待阵列被替换 输出看起来像Q-Javascript承诺等待数组被填充,javascript,asynchronous,q,Javascript,Asynchronous,Q,我是Q库的新手,我对Q对数组方法的承诺有一些理解上的问题。 我有一个数组,其中包含各种键字符串,可以用延迟函数替换。 在成功替换传入的数组之后,我需要继续使用现在完成的数组。 在这种情况下,如何等待阵列被替换 输出看起来像 key: "p", text: "Good morning %text%", inside: key: "p" text: "Good afternoon user" inside: key: "p"
key: "p",
text: "Good morning %text%",
inside: key: "p"
text: "Good afternoon user"
inside: key: "p"
text: "This user has to be replaced."
var object = [];
object.key = "p";
object.text = "Good morning %text%";
object.inside = [];
object.inside.key = "p";
object.inside.text = "Good afternoon %text%";
object.inside.inside = [];
object.inside.inside.key = "p";
object.inside.inside.text = "This %text% has to be replaced.";
goDeep(object, 0);
console.log(object);
function goDeep(data) {
if (data instanceof Object) {
Object.keys(data).forEach(function(key) {
if (data[key] instanceof Object) {
goDeep(data[key]);
} else if (data[key].inside) {
goDeep(data[key].inside);
} else {
var match = scanText(data[key]);
if (match && !(match instanceof Boolean)) {
getServerData(match.input).then (function(response) {
var splitData = data[key].match(/%.*%/)[0].split(",");
for (ii = 0; ii < splitData.length; ++ii) {
splitData[ii] = splitData[ii].trim();
var replaceData = data[key].replace(splitData[ii], response);
// Data gets replaced here
data[key] = replaceData;
};
});
}
}
});
}
}
function scanText(data) {
var match = data.match("(%.*%)", "/g");
if (match) {
return match;
} else {
return false;
}
}
function getServerData(data) {
return Q.delay(1000).thenResolve("user");
}
正如你所看到的,并不是所有的钥匙都被更换了
代码示例
key: "p",
text: "Good morning %text%",
inside: key: "p"
text: "Good afternoon user"
inside: key: "p"
text: "This user has to be replaced."
var object = [];
object.key = "p";
object.text = "Good morning %text%";
object.inside = [];
object.inside.key = "p";
object.inside.text = "Good afternoon %text%";
object.inside.inside = [];
object.inside.inside.key = "p";
object.inside.inside.text = "This %text% has to be replaced.";
goDeep(object, 0);
console.log(object);
function goDeep(data) {
if (data instanceof Object) {
Object.keys(data).forEach(function(key) {
if (data[key] instanceof Object) {
goDeep(data[key]);
} else if (data[key].inside) {
goDeep(data[key].inside);
} else {
var match = scanText(data[key]);
if (match && !(match instanceof Boolean)) {
getServerData(match.input).then (function(response) {
var splitData = data[key].match(/%.*%/)[0].split(",");
for (ii = 0; ii < splitData.length; ++ii) {
splitData[ii] = splitData[ii].trim();
var replaceData = data[key].replace(splitData[ii], response);
// Data gets replaced here
data[key] = replaceData;
};
});
}
}
});
}
}
function scanText(data) {
var match = data.match("(%.*%)", "/g");
if (match) {
return match;
} else {
return false;
}
}
function getServerData(data) {
return Q.delay(1000).thenResolve("user");
}
var对象=[];
object.key=“p”;
object.text=“早上好%text%”;
object.inside=[];
object.inside.key=“p”;
object.inside.text=“下午好%text%”;
object.inside.inside=[];
object.inside.inside.key=“p”;
object.inside.inside.text=“必须替换此%text%;
goDeep(对象,0);
console.log(对象);
功能goDeep(数据){
if(对象的数据实例){
Object.keys(数据).forEach(函数(键){
if(对象的数据[键]实例){
保管员(数据[钥匙]);
}else if(数据[key].inside){
goDeep(数据[键]);
}否则{
变量匹配=扫描文本(数据[键]);
if(匹配&!(匹配布尔值的实例)){
getServerData(match.input).then(函数(响应){
var splitData=data[key]。匹配(/%.*%/)[0]。拆分(“,”;
对于(ii=0;ii
首先让我稍微更正一下您真正想要什么
使用JavaScript promise递归调用异步函数
key: "p",
text: "Good morning %text%",
inside: key: "p"
text: "Good afternoon user"
inside: key: "p"
text: "This user has to be replaced."
var object = [];
object.key = "p";
object.text = "Good morning %text%";
object.inside = [];
object.inside.key = "p";
object.inside.text = "Good afternoon %text%";
object.inside.inside = [];
object.inside.inside.key = "p";
object.inside.inside.text = "This %text% has to be replaced.";
goDeep(object, 0);
console.log(object);
function goDeep(data) {
if (data instanceof Object) {
Object.keys(data).forEach(function(key) {
if (data[key] instanceof Object) {
goDeep(data[key]);
} else if (data[key].inside) {
goDeep(data[key].inside);
} else {
var match = scanText(data[key]);
if (match && !(match instanceof Boolean)) {
getServerData(match.input).then (function(response) {
var splitData = data[key].match(/%.*%/)[0].split(",");
for (ii = 0; ii < splitData.length; ++ii) {
splitData[ii] = splitData[ii].trim();
var replaceData = data[key].replace(splitData[ii], response);
// Data gets replaced here
data[key] = replaceData;
};
});
}
}
});
}
}
function scanText(data) {
var match = data.match("(%.*%)", "/g");
if (match) {
return match;
} else {
return false;
}
}
function getServerData(data) {
return Q.delay(1000).thenResolve("user");
}
注意:我注意到Q库,但我确信它将与其他实现类似
我会尽力简化你的问题,所以我会分步解释
1.如何将一系列承诺转换为单个承诺
您需要all
方法
Q.all([
getWeather({name:'beirut'}),
getWeather({name:'paris'}),
getWeather({name:'madrid'})
]).then(function(beirut, paris, madrid){
// here I am sure that the 3 methods where completed
});
2.如何将参数数组转换为承诺数组
使用array.map
['beirut', 'paris', 'madrid'].map(function(city){ return getWeather(city) });
3.实现递归机制
您似乎混淆了数组和对象。空数组文字如下所示:
[]
。空对象如下所示:{}
。通常不应将非数值属性指定给数组,但似乎只将非数值属性指定给数组。