服务器方法中的Meteor调用不是异步

时间:2016-06-23 12:26:10

标签: javascript meteor promise bluebird

我一直在为Meteor中的多个服务器方法调用而苦苦挣扎。我有一个异步方法methodAsync,我在另一个方法method1内调用。但是Meteor.call('methodAsync', arg1, callback)回调会立即触发!它没有等待它的返回值。

  

方法1

Meteor.methods({
    'method1': function(client) {
        return callAsync()
        .then(insertClient)
        .catch(err);

        //////////

        function callAsync() {
            return new Promise(function(resolve, reject) {
                Meteor.call('methodAsync', client, function(err, data) {
                    if (err) return reject(err);
                    return resolve(data);
                });
            });
        }

        function insertClient(data) {
            return Clients.insert({
                firstname: client.firstname,
                lastname: client.lastname,
                email: client.email,
                tel: client.tel,
                address: client.address,
                data: data
            });
        }

        function err(reason) {
            throw new Meteor.Error(reason);
        }
    }
});
  

MethodAsync

Meteor.methods({
    'methodAsync': function(client) {
        return sellsy.api({
            method: 'Client.create',
            params: {
                third: {
                    name: (client.forename && client.name) ? client.forename + ' ' + client.name : 'Person',
                    type: 'person',
                    email: client.email
                },
                contact: client,
                address: {
                    name: client.address.name || 'Principale',
                    part1: client.address.part1,
                    part2: client.address.part2,
                    zip: client.address.zip,
                    town: client.address.town,
                    countrycode: client.address.countrycode
                }
            }
        })
        .then(response);

        //////////

        function response(data) {
            if (data.error)
                throw new Meteor.Error(data.error);

            return data.response;
        }
    }
});

有什么建议吗?

0 个答案:

没有答案