meteor-feature-requests icon indicating copy to clipboard operation
meteor-feature-requests copied to clipboard

Collection insert, update and remove methods return promise

Open devagrawal09 opened this issue 7 years ago • 2 comments

Collection methods (insert, update, upsert and remove) return a promise instead of value, and don't take callbacks anymore. Eg -

Tasks.insert({ title: 'Buy a Unicorn' }, function( error, id ){
    if( error ) {
        //handle error here
    }
    //do something with id
})

becomes

Tasks.insert({ title: 'Buy a Unicorn' }).then(function( id ){
    //do something with id
}).catch(function( error ){
    //handle error here
})

But modifying the existing methods breaks backwards compatibility, so instead of making the methods return promises instead of values, create new methods that return promises, leaving the old ones as they are. Eg-

Tasks.insertPromisified({ title: 'Buy a Unicorn' }).then(function( id ){
    //do something with id
}).catch(function( error ){
    //handle error here
})

Possible Solution Create a package that uses node's util.promisify() on existing methods on the Mongo.Collection class and stores the promisified methods with new names.

devagrawal09 avatar Apr 05 '18 20:04 devagrawal09

Or async/await:

try {
    const id = await Tasks.insert({ title: Buy a Unicorn});
    // do something with id...
} catch (error) {
    // handle error here
}

laddi avatar Sep 10 '18 22:09 laddi

This would be great! It would be a first step of trying to get away of Fibers. I always encounter situations where my code does not behave as I want it because of some Fibers-magic ... Code that looks synchronous, but runs asynchronous ...

SimonSimCity avatar Apr 24 '19 09:04 SimonSimCity