蓝鸟中的递归承诺不返回

Recursive Promise in Bluebird not returning

我读过 Produce a promise which depends on recursive promises 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"
   }
}

因此,我正在使用 promises 和递归来尝试这个,但我无法弄清楚我的 promise 出了什么问题。我得到了这个结果

{
   "id": e872530a-27fc-4263-ad39-9a21568c31ef,
   "name": "dood",
   "friendId": {
      "isFulfilled": False,
      "isRejected": False
   },
   "motherId": {
      "isFulfilled": False,
      "isRejected": False
   }
}

我使用的是 bluebird 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;})
}  

尝试改变

obj[property] = getExpandedObj(uuid, recursiveLevel-1)
   .then(function(exObj) {
     return exObj;
   });

return getExpandedObj(uuid, recursiveLevel-1)
 .then(function(exObj) {
    obj[property] = exObj;
    return obj;
});

因为您将值设置为承诺对象而不是实际值。

问题在于 a) 您的映射函数 return 什么都不做,因此不会等待 b) 您根本不应该在这里使用 map

完美的解决方案是 Bluebird 的 Promise.props,它等待对象属性的承诺。

function getUuid(value) {
     if (typeof value != "string") return null;
     var uuid = value.match(/\w{8}(-\w{4}){3}-\w{12}?/);
     if (uuid) return uuid[0];
     return null;
}
function getExpandedObj(uuid, recursiveLevel) {
    return getObj(uuid).then(function(obj) {
        // prevent infinite recursion
        if (recursiveLevel <= 0)
            return obj;
        for (var property in obj) {
            var uuid = getUuid(obj[property])
            if (uuid) {
                obj[property] = getExpandedObj(uuid, recursiveLevel-1);
            }
        }
        return Promise.props(obj);
    });
}