简体   繁体   中英

JavaScript callback and promise confusion

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"?

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 => {
    // …
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM