Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 然后承诺不等待结果返回_Node.js_Promise_Q - Fatal编程技术网

Node.js 然后承诺不等待结果返回

Node.js 然后承诺不等待结果返回,node.js,promise,q,Node.js,Promise,Q,以下是node.js中使用promise的函数 exports.getDetailsByUserId = function(req,res) { getUserDetails(req, res) // function to get user details .then(function (Details) { var allpromises = Details.map(getStatus); //get and update the status of use

以下是node.js中使用promise的函数

exports.getDetailsByUserId = function(req,res) {
     getUserDetails(req, res) // function to get user details
    .then(function (Details) {
        var allpromises = Details.map(getStatus); //get and update the status of user
        return allpromises
    })
    .then(function (promises) {
        var orderresult =[];
        Q.allSettled(promises)
        .then(function (allpromises) {
            orderresult.push(allpromises);
        })
        return orderresult;
    })
    .then(function (result) {
        getUserDetails(req, res)//again calling get user details so that i will get modified result
       .then(function (data) {
            res.json({                             
                "User" : data
            })
        })
    })
    .catch(function (error) {
        console.log(error);
        res.status(200).json({
            "Message": error
        })
    })
}

var getStatus = function (data,req) {
    var deferred = Q.defer();
        requestify.post('http://example.com', {
            "data": [{ "Number": No }]
        })
    .then(function (response) {
            response.getBody();
            response.body;
            var Status = JSON.parse(response.body);
          var Details = { "_id": data._id, "Status": Status.status[0] };
          return OrderDetails;
    })
    .then(function (DetailsData){
        var req = { body : { Details :DetailsData} };
       updateDetails(req).then(function (data) { 
          deferred.resolve;
        })
    })
   .catch(function (error) {                     
        deferred.resolve(error);
   });    
    return deferred.promise;
}
函数第一次获取用户的详细信息,然后调用另一个API并更新相关的用户状态。在这一点上,一切都很顺利

我需要第二次调用
getUserDetails
以获取更新结果,但它不会给出更新结果。也许在更新状态之前会调用它?代码可能有什么问题,以便在
Q.all
之后执行

多谢各位

你爱上了经典问题。
then
回调是异步的,
orderresult
返回时将不包含
allpromises
数组。要让某些东西等待承诺,你必须
返回它

在任何情况下,您都应该将代码简化为

getUserDetails(req, res)
.then(function (details) {
    var allpromises = details.map(getStatus);
    return Q.allSettled(allpromises); // just call it here already and return a promise
})
.then(function (allpromiseresults) {
    var orderresult = [allpromiseresults]; // not sure what this is good for
    return orderresult;
})
.then(function (result) { // given that you don't use the `result` anyway
    return getUserDetails(req, res);
}).then(function (data) {
    res.json({                             
        "User" : data
    })
}, function (error) {
    console.log(error);
    res.status(200).json({
        "Message": error
    })
});
getStatus
中,您应该避免:


orderresult
有什么意义?要将数组包装到数组中,不需要调用
getUserDetails
两次。只是更新一下,你不知道它们已经是什么了吗?orderresult是一个承诺数组。我想得到更新后的结果显示在girdNo它不是。这很可能是您的问题。在第二次调用
getUserDetails(req,res)
之前,您没有等待
Q.allsolled(promises)
完成。另一个异步javascript问题…感谢您宝贵的时间和广泛的回答,但这仍然会在Q.allsolited(allpromises)返回并给出未修改的结果之前执行对“getUserDetails”的第二次调用。现在我尝试在所有的Q之后调用updateDetails。AllSetted(AllPromissions)希望它能工作,而不是调用getStatus。@sam如果这样做,您可能忘记了某个地方的
返回值。我的代码当然不会在
allSettled
返回后调用第二个
getUserDetails
,它甚至会等待承诺实现。是什么给你的印象是它“以前”发生过?也许
updateDetails
中有一个bug(您没有显示)。请确保它返回的承诺有效。请修改上述代码以在中处理。它现在有效。@sam您不应编辑我的答案(除了明显的错误或改进),而应发表评论。考虑到这确实涉及延迟的
,您似乎没有使用我的答案-您根本不需要任何延迟!请阅读反模式上的链接。
getUserDetails(req, res)
.then(function (details) {
    var allpromises = details.map(getStatus);
    return Q.allSettled(allpromises); // just call it here already and return a promise
})
.then(function (allpromiseresults) {
    var orderresult = [allpromiseresults]; // not sure what this is good for
    return orderresult;
})
.then(function (result) { // given that you don't use the `result` anyway
    return getUserDetails(req, res);
}).then(function (data) {
    res.json({                             
        "User" : data
    })
}, function (error) {
    console.log(error);
    res.status(200).json({
        "Message": error
    })
});
function getStatus(data,req) {
    return requestify.post('http://example.com', {
        "data": [{ "Number": No }]
    })
    .then(function (response) {
        response.getBody(); // ???
        var status = JSON.parse(response.body);
        var details = { "_id": data._id, "Status": Status.status[0] };
        // no `OrderDetails`, no `DetailsData`

        var req = {body: {Details: details}};
        return updateDetails(req) // probably "getStatus" should be called "updateStatus"
    });
}