Sequelize中的承诺:如何从每个承诺中获得结果

时间:2014-02-12 00:49:36

标签: javascript promise sequelize.js bluebird

在Sequelize> = 1.7 we can use promises

您能否向我解释如何从此代码中获取每个用户的值:

var User = sequelize.define("user", {
  username: Sequelize.STRING
})


User
  .sync({ force: true })
  .then(function() { return User.create({ username: 'John' }) })
  .then(function(john) { return User.create({ username: 'Jane' }) })
  .then(function(jane) { return User.create({ username: 'Pete' }) })
  .then(function(pete) {
    console.log("we just created 3 users :)")
    console.log("this is pete:")
    console.log(pete.values)

    // what i want:
    console.log("this is jane:")
    console.log(jane.values)

    console.log("this is john:")
    console.log(john.values)
  })

UPD

所有值都需要与其他模型设置关联。 其实我需要一些像这样的代码:

User.hasMany(Group)
Group.hasMany(User)

User
  .sync({ force: true })
  .then(function() { return User.create({ username: 'John' }) })
  .then(function(john) { return User.create({ username: 'Jane' }) })
  .then(function(jane) { return User.create({ username: 'Pete' }) })
  .then(function(pete) { return Group.findOrCreate({id: 1}) })
  .then(function(group) {return group.setUsers([john, jane, pete])})
  .then(function(result) { console.log(result)})
})

3 个答案:

答案 0 :(得分:21)

蓝鸟的方式是collection helper functions

如果您想并行创建它们,请使用map

User.sync({ force: true })
  .then(function() {
    return Promise.map( ['John', 'Jane', 'Pete'], function(name) {
      return User.create({ username: name });
    })
  }).spread(function(john, jane, pete) {
    console.log("we just created 3 users :)")
    console.log("this is john:")
    console.log(john.values)
    console.log("this is jane:")
    console.log(jane.values)
    console.log("this is pete:")
    console.log(pete.values)
  })

如果您需要连续创建它们,只需将其更改为mapSeries(3.0 +)。

如果数组不需要是动态的,并且您只想通过承诺链传递共享值(如示例中所示),请查看How do I access previous promise results in a .then() chain?

答案 1 :(得分:4)

不使用其他库(如果需要维护创建的顺序),可以通过在封闭范围内创建包含值的变量来实现:

var created = {};
User
  .sync({ force: true })
  .then(function() { return User.create({ username: 'John' }) })
  .then(function(john) { created.john = john; return User.create({ username: 'Jane' }) })
  .then(function(jane) { created.jane = jane; return User.create({ username: 'Pete' }) })
  .then(function(pete) {
    created.pete = pete;

    console.log("we just created 3 users :)")
    console.log("this is pete:")
    console.log(created.pete.values)

    // what i want:
    console.log("this is jane:")
    console.log(created.jane.values)

    console.log("this is john:")
    console.log(created.john.values)
  })

总的来说,我建议你倾向于@ Bergi的答案,它会创建一个Promise列表并等待所有的承诺完成。

根据问题更新进行修改:

使用更新的代码块并构建@ Bergi对Promise.map的建议,您可以避免在更高的范围内使用变量,如下所示:

User.hasMany(Group)
Group.hasMany(User)

User
  .sync({ force: true })
  .then(function() {
    var users = Promise.map( ['John', 'Jane', 'Pete'], function(name) {
      return User.create({ username: name });
    });
    var group = Group.findOrCreate({id: 1});
    return Promise.all([group, users]);
  })
  .spread(function(group, users) {return group.setUsers(users)})
  .then(function(result) { console.log(result)})
})

答案 2 :(得分:3)

试试这个......

User
    .sync({ force: true })
    .then(function () {
        return User.create({ username: 'John' });
    })
    .then(function (john) {
        console.log("this is john:");
        console.log(john.values);
        return User.create({ username: 'Jane' });
    })
    .then(function (jane) {
        console.log("this is jane:");
        console.log(jane.values);
        return User.create({ username: 'Pete' });
    })
    .then(function (pete) {
        console.log("we just created 3 users :)");
        console.log("this is pete:");
        console.log(pete.values);
    });