0

I am using a async await to make a db call

let result = await cb.addDoc(req.bucket,docKey,newContact)

the function i call is a promise and works fine but i am having an issue accessing the second part of resolve. my result is always the docId. How can i get access to to the result in resolver ?

const addDoc = (bucket,docID,doc)=>{
    return new Promise((resolve,reject)=>{
      bucket.insert(docID,doc,(err, result)=>{
        if(err) return reject(err);
        return resolve(docID ,result);

      });
    });
  }
MisterniceGuy
  • 1,197
  • 7
  • 25

2 Answers2

2

A promise inherently means a value resolved over time, so you cannot resolve a promise with more than one property. But you can create an object or array and resolve using it.

let { docID, result } = await cb.addDoc(req.bucket, docKey, newContact);

const addDoc = (bucket, docID, doc) => {
  return new Promise((resolve, reject) => {
    bucket.insert(docID, doc, (err, result) => {
      if (err) return reject(err);
      return resolve({ docID, result });
    });
  });
};
PrivateOmega
  • 1,708
  • 10
  • 23
  • just for good measures, how would i access it if it would be an array, not that i need it but its good to know. The object can hold arrays so not sure when i would need to return an array directly vs an object with an array – MisterniceGuy Mar 27 '19 at 05:44
1

Return an Array or object instead

 const addDoc = (bucket,docID,doc)=>{
return new Promise((resolve,reject)=>{
  bucket.insert(docID,doc,(err, result)=>{
    if(err) return reject(err);
    return resolve([docID ,result]);

  });
});
}


 const addDoc = (bucket,docID,doc)=>{
return new Promise((resolve,reject)=>{
  bucket.insert(docID,doc,(err, result)=>{
    if(err) return reject(err);
    return resolve({docID ,result});

  });
});
}
Jew
  • 476
  • 2
  • 8