M
M
Mikhailo Poberezhny2017-03-31 11:24:25
JavaScript
Mikhailo Poberezhny, 2017-03-31 11:24:25

ES6 class method?

The crux of the problem is this.

Class a {
     constructor() {}
     getCollection() {
         let collection = db.collection('users').find((err,users)=>{
             return users
        })
         return collection
    }
}

How can I wait for the callback to complete and return? Otherwise, it turns out that the empty collection returns from the method before the callback completes.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
N
Nicholas, 2017-03-31
@healqq

Node 7 can do async/await. Well, or in promises at worst.

V
Vladislav, 2017-03-31
@Div100

Thus, don’t do a return, instead do the actions that you will do with collections right away in find, as soon as it works like that, and perform your actions

S
Stepanya, 2017-03-31
@Stepanya

Maybe your collection is empty because

return users
returns undefined?
Look at the specification of the find method

N
Negwereth, 2017-03-31
@Negwereth

Async : www.sohamkamani.com/blog/2016/03/14/wrapping-your-...
ES6 Promises: https://developer.mozilla.org/ru/docs/Web/JavaScri...
ES7 async/await : https://jakearchibald.com/2014/es7-async-functions/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question