0

I am learning about promises and updating documents in mongoose. I have a promise that is working when I include a "resolve" argument but not without that argument, and I can't figure out how to remove it for purposes of streamlining the code. I checked this question and this one about .save() but still don't understand if this is possible - I am still learning the ins and outs of promises. I got it to work with a Promise.all but not a single Promise. Here is the working request:

function updateQuantities(){
    return new Promise((resolve)=>{
        var conditions = {'titleByDate.instanceId'  : {$ne: null}};
        var update     = {'titleByDate.currentQuantity':  0,'titleByDate.newTitle':  0};
        resolve(titleRecords.update(conditions, update, options));
    })
}

updateQuantities().then(/*a bunch of other promises*/)

And this one does not work, it gives an 'unexpected token var' error:

function updateQuantities(){
    return new Promise(
        var conditions = {'titleByDate.instanceId'  : {$ne: null}};
        var update     = {'titleByDate.currentQuantity':  0,'titleByDate.newTitle':  0};
        return titleRecords.update(conditions, update, options);
    )
}
updateQuantities().then(/*a bunch of other promises*/)

Is there any way I can write the bottom one so that it works and I don't have to use resolve()? Thanks.

garson
  • 1,505
  • 3
  • 22
  • 56
  • Does `titleRecords.update` return a promise? If so, just return that and remove the `return new Promise` wrapper. – 4castle Jul 13 '17 at 15:16
  • @4castle I don't think so - tried that but it says 'then' isn't valid so i assume it doesn't return a promise – garson Jul 13 '17 at 15:17
  • @naomik I tried cutting out the (resolve) and making it just () but then the promise doesn't seem to be resolving. What confuses me is I was above to make a set of Promise.all without ever typing 'resolve' elsewhere, so I thought there might be a way to do this. – garson Jul 13 '17 at 15:19
  • @4castle nevermind you were totally right. I don't know what I was doing before but now it works removing that wrapper. Easy. Thanks – garson Jul 13 '17 at 15:24
  • Yeah it's a duplicate - I just didn't find that one in my searches. Thanks – garson Jul 13 '17 at 15:27
  • 1
    No worries! Glad I could help. I believe you can approve the duplicate if you wish to close the question. – 4castle Jul 13 '17 at 15:28

0 Answers0