문제

New to q, I got a sample code as follow, what it does is, get an order, then get the owner of the order, append the name of the owner to the order so that the complete order can be displayed, how to change this to using Q (kriskowal/q)? Thanks,

db.get_order(order_id, function(err, order) {
users.get_user(order.user_id, function(err, user) {
    order['$user_name'] = user.name
    console.log(order)
  })
})
도움이 되었습니까?

해결책

I am assuming that get_order function depends on the db object and also get_user depends on user object. If that is the case, then you need to invoke those methods with Q.ninvoke, like this

Q.ninvoke(db, "get_order", order_id)
.then(function(order) {
    return Q.ninvoke(user, "get_user", order.user_id);
})
.then(function(user) {
    console.log(user.name);
})
.catch(function(err) {
    console.error(err);
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top