Skip to content

Commit 0e79c5c

Browse files
authoredSep 1, 2021
Merge pull request #10633 from AbdelrahmanHafez/prefer-async-await
get rid of co
2 parents 09dae52 + c4b0e86 commit 0e79c5c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+8433
-9237
lines changed
 

‎examples/statics/statics.js

+20-29
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,25 @@ require('./person.js')();
99
const Person = mongoose.model('Person');
1010

1111
// connect to a server to do a quick write / read example
12-
13-
mongoose.connect('mongodb://localhost/persons', function(err) {
14-
if (err) {
15-
throw err;
16-
}
17-
18-
Person.create({ name: 'bill', age: 25, birthday: new Date().setFullYear((new Date().getFullYear() - 25)) },
19-
function(err, bill) {
20-
if (err) {
21-
throw err;
22-
}
23-
console.log('People added to db: %s', bill.toString());
24-
25-
// using the static
26-
Person.findPersonByName('bill', function(err, result) {
27-
if (err) {
28-
throw err;
29-
}
30-
31-
console.log(result);
32-
cleanup();
33-
});
34-
}
35-
);
36-
});
37-
38-
function cleanup() {
39-
Person.remove(function() {
40-
mongoose.disconnect();
12+
run().catch(console.error);
13+
14+
async function run() {
15+
await mongoose.connect('mongodb://localhost/persons');
16+
const bill = await Person.create({
17+
name: 'bill',
18+
age: 25,
19+
birthday: new Date().setFullYear((new Date().getFullYear() - 25))
4120
});
21+
console.log('People added to db: %s', bill.toString());
22+
23+
// using the static
24+
const result = await Person.findPersonByName('bill');
25+
26+
console.log(result);
27+
cleanup();
28+
}
29+
30+
async function cleanup() {
31+
await Person.remove();
32+
mongoose.disconnect();
4233
}

‎lib/document.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -2074,14 +2074,14 @@ Document.prototype.$isDefault = function(path) {
20742074
* Getter/setter, determines whether the document was removed or not.
20752075
*
20762076
* ####Example:
2077-
* product.remove(function (err, product) {
2078-
* product.$isDeleted(); // true
2079-
* product.remove(); // no-op, doesn't send anything to the db
2077+
* const product = await product.remove();
2078+
* product.$isDeleted(); // true
2079+
* product.remove(); // no-op, doesn't send anything to the db
2080+
*
2081+
* product.$isDeleted(false);
2082+
* product.$isDeleted(); // false
2083+
* product.remove(); // will execute a remove against the db
20802084
*
2081-
* product.$isDeleted(false);
2082-
* product.$isDeleted(); // false
2083-
* product.remove(); // will execute a remove against the db
2084-
* })
20852085
*
20862086
* @param {Boolean} [val] optional, overrides whether mongoose thinks the doc is deleted
20872087
* @return {Boolean} whether mongoose thinks this doc is deleted.
@@ -2161,10 +2161,9 @@ Document.prototype.isInit = function(path) {
21612161
*
21622162
* ####Example
21632163
*
2164-
* Thing.findOne().select('name').exec(function (err, doc) {
2165-
* doc.isSelected('name') // true
2166-
* doc.isSelected('age') // false
2167-
* })
2164+
* const doc = await Thing.findOne().select('name');
2165+
* doc.isSelected('name') // true
2166+
* doc.isSelected('age') // false
21682167
*
21692168
* @param {String|Array<String>} path
21702169
* @return {Boolean}

0 commit comments

Comments
 (0)
Please sign in to comment.