我讀過 Produce a promise which depends on recursive promises chaining recursive promise with bluebird Recursive Promises? 但我仍然無法找出什麼地方錯了,我怎麼構建我的諾言遞歸無極藍鳥不返回
來自所以我獲取對象數據庫。每個對象都有一個字段,但有時它具有對UUID的引用。因此,舉例來說,如果一個人有一個朋友和母親,它會像
{
"id": e872530a-27fc-4263-ad39-9a21568c31ef,
"name": "dood",
"friendId": "571746fc-686d-4170-a53e-7b7daca62fa0",
"motherId": "99b65849-1f1c-4881-a1d0-c5ae432e83a2"
}
現在的想法是,當我取的對象我想替換任何其他UUID與擴展版本。
{
"id": e872530a-27fc-4263-ad39-9a21568c31ef,
"name": "dood",
"friendId": {
"id": "571746fc-686d-4170-a53e-7b7daca62fa0",
"name": "peter"
},
"motherId": {
"id": "99b65849-1f1c-4881-a1d0-c5ae432e83a2",
"name": "ma"
}
}
因此,我使用承諾和遞歸來嘗試這個,但我無法弄清楚我的承諾出了什麼問題。我得到這樣的結果,而不是
{
"id": e872530a-27fc-4263-ad39-9a21568c31ef,
"name": "dood",
"friendId": {
"isFulfilled": False,
"isRejected": False
},
"motherId": {
"isFulfilled": False,
"isRejected": False
}
}
我使用藍鳥JS,這裏是怎樣的代碼看起來像
function getExpandedObj(uuid, recursiveLevel) {
return getObj(uuid) //This gets the object from the database
.then(function(obj) {
//I convert the obj to an array where each element is
//{property:value} so I can do a map over it
// some code to do that, I think it's irrelevant so I'll
// skip it
return objArr;
})
.map(function(obj) {
//prevent infinite recursion
if (recursiveLevel > 0) {
for (var property in obj) {
if (typeof(obj[property]) === "string") {
var uuid = obj[property].match(/(\w{8}(-\w{4}){3}-\w{12}?)/g);
if (uuid != null) {
uuid = uuid[0];
obj[property] = getExpandedObj(uuid, recursiveLevel-1)
.then(function(exObj) { return exObj;})
}
}
}
}
})
.then(function(obj) {return obj;})
}
啊,這就是[你之前要求](http://stackoverflow.com/q/32193946/1048572)。 – Bergi