-2

I have the result of a long lasting operation, a MongoDB query:

const a = collection.find({}).project({ name: 1 }).toArray()

It has the type of Promise<Document[]>

I would do a transformation, but it is not possible:

let a2 = a.map((i) => {i._id, { name: i.name }})

In Vapor / Swift, there is a map not only for array, but also for promises, what about in JS / TS?

How can I apply further operation on the Promise<Document[]>?

János
  • 32,867
  • 38
  • 193
  • 353

2 Answers2

0

For Promises, you need to use .then for functionality and .catch for errors.

For example(reference):-

const promise = new Promise((resolved, rejected) => {
setTimeout(() =>{
console.log(rejected)
},1000)
})
promise.then((value) => console.log(value)).catch((err) => console.log(err));

And always remember to write return when you are using a map and using {} because otherwise, you will get errors then.

Shilpe Saxena
  • 219
  • 2
  • 8
-1

You can use the .then() function on different parts of the code

Auloma
  • 101
  • 1
  • 8
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 23 '22 at 03:15