不要怂,就是干,撸起袖子干!

Commit 18bcb97e by Felix Becker

Fixes usage of promise antipatterns in the docs

1 parent f482790b
...@@ -53,7 +53,8 @@ sequelize ...@@ -53,7 +53,8 @@ sequelize
.authenticate() .authenticate()
.then(function(err) { .then(function(err) {
console.log('Connection has been established successfully.'); console.log('Connection has been established successfully.');
}, function (err) { })
.catch(function (err) {
console.log('Unable to connect to the database:', err); console.log('Unable to connect to the database:', err);
}); });
``` ```
......
...@@ -510,12 +510,12 @@ Project.create({ /* */ }).then(function(project) { ...@@ -510,12 +510,12 @@ Project.create({ /* */ }).then(function(project) {
// check if all associated objects are as expected: // check if all associated objects are as expected:
// let's assume we have already a project and two users // let's assume we have already a project and two users
project.setUsers([user1, user2]).then(function() { project.setUsers([user1, user2]).then(function() {
return project.hasUsers([user1]).then(function(result) { return project.hasUsers([user1]);
}).then(function(result) {
// result would be false // result would be false
return project.hasUsers([user1, user2]).then(function(result) { return project.hasUsers([user1, user2]);
}).then(function(result) {
// result would be true // result would be true
})
})
}) })
``` ```
......
...@@ -113,10 +113,9 @@ Once you created an object and got a reference to it, you can delete it fr ...@@ -113,10 +113,9 @@ Once you created an object and got a reference to it, you can delete it fr
```js ```js
Task.create({ title: 'a task' }).then(function(task) { Task.create({ title: 'a task' }).then(function(task) {
// now you see me... // now you see me...
  return task.destroy();
task.destroy().then(function() { }).then(function() {
// now i'm gone :)  // now i'm gone :)
})
}) })
``` ```
...@@ -144,9 +143,9 @@ User.bulkCreate([ ...@@ -144,9 +143,9 @@ User.bulkCreate([
{ username: 'foo', isAdmin: true }, { username: 'foo', isAdmin: true },
{ username: 'bar', isAdmin: false } { username: 'bar', isAdmin: false }
]).then(function() { // Notice: There are no arguments here, as of right now you'll have to... ]).then(function() { // Notice: There are no arguments here, as of right now you'll have to...
User.findAll().then(function(users) { return User.findAll();
}).then(function(users) {
console.log(users) // ... in order to get the array of user objects console.log(users) // ... in order to get the array of user objects
})
}) })
``` ```
...@@ -158,15 +157,15 @@ Task.bulkCreate([ ...@@ -158,15 +157,15 @@ Task.bulkCreate([
{subject: 'reading', status: 'executing'}, {subject: 'reading', status: 'executing'},
{subject: 'programming', status: 'finished'} {subject: 'programming', status: 'finished'}
]).then(function() { ]).then(function() {
Task.update( return Task.update(
{ status: 'inactive' } /* set attributes' value */, { status: 'inactive' }, /* set attributes' value */,
{ where: { subject: 'programming' }} /* where criteria */ { where: { subject: 'programming' }} /* where criteria */
).then(function(affectedRows) { );
}).then(function(affectedRows) {
// affectedRows will be 2 // affectedRows will be 2
Task.findAll().then(function(tasks) { return Task.findAll();
}).then(function(tasks) {
console.log(tasks) // the 'programming' tasks will both have a status of 'inactive' console.log(tasks) // the 'programming' tasks will both have a status of 'inactive'
})
})
}) })
``` ```
...@@ -178,17 +177,17 @@ Task.bulkCreate([ ...@@ -178,17 +177,17 @@ Task.bulkCreate([
{subject: 'reading', status: 'executing'}, {subject: 'reading', status: 'executing'},
{subject: 'programming', status: 'finished'} {subject: 'programming', status: 'finished'}
]).then(function() { ]).then(function() {
Task.destroy({ return Task.destroy({
where: { where: {
subject: 'programming' subject: 'programming'
}, },
truncate: true /* this will ignore where and truncate the table instead */ truncate: true /* this will ignore where and truncate the table instead */
}).then(function(affectedRows) { });
}).then(function(affectedRows) {
// affectedRows will be 2 // affectedRows will be 2
Task.findAll().then(function(tasks) { return Task.findAll();
}).then(function(tasks) {
console.log(tasks) // no programming, just reading :( console.log(tasks) // no programming, just reading :(
})
})
}) })
``` ```
...@@ -294,27 +293,27 @@ First of all you can define a field and the value you want to add to it. ...@@ -294,27 +293,27 @@ First of all you can define a field and the value you want to add to it.
```js ```js
User.findById(1).then(function(user) { User.findById(1).then(function(user) {
user.increment('my-integer-field', {by: 2}).then(/* ... */) return user.increment('my-integer-field', {by: 2})
}) }).then(/* ... */)
``` ```
Second, you can define multiple fields and the value you want to add to them. Second, you can define multiple fields and the value you want to add to them.
```js ```js
User.findById(1).then(function(user) { User.findById(1).then(function(user) {
user.increment([ 'my-integer-field', 'my-very-other-field' ], {by: 2}).then(/* ... */) return user.increment([ 'my-integer-field', 'my-very-other-field' ], {by: 2})
}) }).then(/* ... */)
``` ```
Third, you can define an object containing fields and its increment values. Third, you can define an object containing fields and its increment values.
```js ```js
User.findById(1).then(function(user) { User.findById(1).then(function(user) {
user.increment({ return user.increment({
'my-integer-field': 2, 'my-integer-field': 2,
'my-very-other-field': 3 'my-very-other-field': 3
}).then(/* ... */) })
}) }).then(/* ... */)
``` ```
## Decrementing certain values of an instance ## Decrementing certain values of an instance
...@@ -325,25 +324,25 @@ First of all you can define a field and the value you want to add to it. ...@@ -325,25 +324,25 @@ First of all you can define a field and the value you want to add to it.
```js ```js
User.findById(1).then(function(user) { User.findById(1).then(function(user) {
user.decrement('my-integer-field', {by: 2}).then(/* ... */) return user.decrement('my-integer-field', {by: 2})
}) }).then(/* ... */)
``` ```
Second, you can define multiple fields and the value you want to add to them. Second, you can define multiple fields and the value you want to add to them.
```js ```js
User.findById(1).then(function(user) { User.findById(1).then(function(user) {
user.decrement([ 'my-integer-field', 'my-very-other-field' ], {by: 2}).then(/* ... */) return user.decrement([ 'my-integer-field', 'my-very-other-field' ], {by: 2})
}) }).then(/* ... */)
``` ```
Third, you can define an object containing fields and its decrement values. Third, you can define an object containing fields and its decrement values.
```js ```js
User.findById(1).then(function(user) { User.findById(1).then(function(user) {
user.decrement({ return user.decrement({
'my-integer-field': 2, 'my-integer-field': 2,
'my-very-other-field': 3 'my-very-other-field': 3
}).then(/* ... */) })
}) }).then(/* ... */)
``` ```
...@@ -24,10 +24,10 @@ sequelize.sync().then(function() { ...@@ -24,10 +24,10 @@ sequelize.sync().then(function() {
return User.create({ return User.create({
username: 'janedoe', username: 'janedoe',
birthday: new Date(1980, 6, 20) birthday: new Date(1980, 6, 20)
}).then(function(jane) { });
}).then(function(jane) {
console.log(jane.get({ console.log(jane.get({
plain: true plain: true
})) }));
});
}); });
``` ```
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!