meteor-feature-requests
meteor-feature-requests copied to clipboard
Collection insert, update and remove methods return promise
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.
Or async/await:
try {
const id = await Tasks.insert({ title: Buy a Unicorn});
// do something with id...
} catch (error) {
// handle error here
}
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 ...