How to make a call to find() synchronous?


#1

I have the following ES6 class and need to wait for the getAppearances function to return before the program flow continues, i.e. synchronously - how do I do this?

class MyClass {

    constructor() {
    }

    getAppearances(id) {
        return MyColl.find({
            ...    
        }).fetch();
    }

}
export { MyClass as default};


const c = new MyClass;
if( c.getAppearances > 0 ){
    ....
}
else{
    ...
}

#2

use Meteor.wrapAsync

    //turn async function into synchronous
    asyncFunc = function (callback) {
            //async stuff here
        MyColl().find('stuff', function () {
            callback();
        });
    }
    syncFunc = Meteor.wrapAsync(asyncFunc);
    syncFunc();

#3

Is callback necessary?


#4

yes, just call it wen you thing your sync function should end (can be in multiple places)


#5

I discovered that .find() is synchronous by design.