0

I'm trying to understand a API reference description and I'm having trouble understanding what it means:

http://mongodb.github.io/node-mongodb-native/2.2/api/Cursor.html#toArray

On the top it says

'toArray(callback) -> Promise'

I know the callback is equivalent to a "Block" but what does it mean to have an arrow sign pointing to "Promise"?

mskw
  • 10,063
  • 9
  • 42
  • 64

1 Answers1

2

That means that the function can either take a callback, or return a Promise. As explained in the documentation.

Returns:
Promise if no callback passed

So you can either call that function passing a callback:

acursor.toArray(anarray => {
  // …
});

Or get the result using the returned Promise:

acursor
  .toArray()
  .then(anarray => {
    // …
  });
Bertrand Marron
  • 21,501
  • 8
  • 58
  • 94
  • why would i want to use a promise when I can just do get the result on the callback? – mskw May 28 '17 at 15:31
  • Seem like I'm writing extra code, but doing the same thing. – mskw May 28 '17 at 15:31
  • 1
    This might be of interest: https://stackoverflow.com/questions/22539815/arent-promises-just-callbacks – Bertrand Marron May 28 '17 at 15:32
  • Do you think this is also valid? The answer given https://stackoverflow.com/questions/14244775/what-is-the-difference-between-callback-and-promise?noredirect=1&lq=1 – mskw May 28 '17 at 15:35
  • It’s up to you to use either of the two APIs, they’ll both work and do what you want to achieve. – Bertrand Marron May 28 '17 at 15:38