0

using bluebird q, I have this:

var myBill
db.getBillAsync().then(function (bill) {
myBill = bill
return users.find_user_by_idAsync(bill.user_id)
}).then(function (user) {
   myBill.user_name = user.name

console.log(myBill)

})

the purpose of the code above is to get the name of user and add it as to bill, this works well, now if I have a list of bills, how to get the names of all the bills and assign to myBills? using a loop? or bluebird has some other approaches?

var myBills
db.getBillsAsync().then(function (bills) {
  myBills = bills
  return users.find_user_by_idAsync(bill.user_id) ?
}).then(function (user) {
   ?
})

1 Answer 1

1

It will look something like this:

db.getBillsAsync().map(function (bill) {
  return users.find_user_by_idAsync(bill.user_id)
    .then(function (user) {
      bill.user_name = user.name;
      return bill;
    });
}).then(function (bills) {
// you got your bills with user names
})
2
  • thanks, but bill.user_name = user.name seems not referring to the 'bill' in map?
    – AngeloC
    May 11, 2014 at 15:33
  • 2
    why? there is no other bill in the scope May 11, 2014 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.