Parse.com承诺实施

时间:2014-08-14 19:34:04

标签: parse-platform promise

我正在尝试执行以下操作,我的问题是内联注释的。如何将registeredUsersRole和来自query.find()的用户发送到链接中的下一个用户而不在那里创建嵌套呢?

// ...
registeredUsersRoleQuery.first({
    useMasterKey: true
}).
then(function(registeredUsersRole) {
    // This means that role was found, so simply return it to the next promise
    return registeredUsersRole;
}, function() {
    // This means that role wasn't found, so create it and return it to the next promise
    var registeredUsersRoleAcl = new Parse.ACL();
    registeredUsersRoleAcl.setPublicReadAccess(false);
    registeredUsersRoleAcl.setPublicWriteAccess(false);
    return new Parse.Role(registeredUsersRoleName, registeredUsersRoleAcl).save{
        useMasterKey: true
    });
}).
then(function(registeredUsersRole) {
    var query = new Parse.Query(Parse.User);
    query.equalTo('verificationCodeVerified', true);

    // How can I send both the registeredUsersRole and the users from 
    // query.find() to the next then in the chain without creating 
    // a nested then in here?
    query.find({
        useMasterKey: true
    }).
    then(function(allVerifiedUsers) {
        registeredUsersRole.getUsers().
        add(allVerifiedUsers);
        return registeredUsersRole.save();
    });
}).
then(function() {
    // How do I have both the registeredUsersRole and the users here?
});

1 个答案:

答案 0 :(得分:1)

您可以使用Parse.Promise.when

registeredUsersRoleQuery.first({ useMasterKey: true }).
then(null , function() {// null ignores
    // ...
    return new Parse.Role(...).save({useMasterKey: true });
}).
then(function(registeredUsersRole) {
    var query = new Parse.Query(Parse.User);
    query.equalTo('verificationCodeVerified', true);
    return Promise.when([
        registeredUsersRole, 
        query.find({ useMasterKey: true })
    ]);
}).
then(function(registeredUsersRole, allVerifiedUsers) {
     // access both here, no nesting was needed
});