Created
March 26, 2014 17:37
-
-
Save yamadapc/9788938 to your computer and use it in GitHub Desktop.
mongoose + promises experiments - https://github.com/yamadapc/mongoose-bluebird-utils/issues/1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var mongoose = require('mongoose'), | |
Promise = require('bluebird'); | |
mongoose.connect('mongodb://localhost/test'); | |
var SomethingSchema = new mongoose.Schema({ | |
name: String | |
}); | |
SomethingSchema.pre('save', function(next) { | |
this.name = 'something'; | |
next(); | |
}); | |
var Something = mongoose.model('Something', SomethingSchema); | |
Promise.promisifyAll(mongoose.Model); | |
Promise.promisifyAll(mongoose.Model.prototype); | |
/** | |
* Using saveAsync | |
* --------------------------------------------------------------------------*/ | |
var somethingAsync = new Something({ name: 'asdf' }); | |
somethingAsync.saveAsync() | |
.spread(function(something) { | |
console.log('Using `saveAsync` =>', something.name); // asdf | |
}); | |
/** | |
* Using save | |
* --------------------------------------------------------------------------*/ | |
var something = new Something({ name: 'asdf' }); | |
something.save(function(err, something) { | |
console.log('Using `save` =>', something.name); // something | |
}); | |
/** | |
* Using Something.prototype.save.call | |
* --------------------------------------------------------------------------*/ | |
var somethingWeird = new Something({ name: 'asdf' }); | |
Something.prototype.save.call(somethingWeird, function(err, something) { | |
console.log('Using `Something.prototype.save.call` =>', something.name); // => asdf | |
}); | |
/** | |
* Using something.save.call | |
* --------------------------------------------------------------------------*/ | |
var somethingWeirder = new Something({ name: 'asdf '}); | |
something.save.call(somethingWeirder, function(err, something) { | |
console.log('something.save.call =>', something.name); // => something | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment