meteor / promise

ES6 Promise polyfill with Fiber support
MIT License
63 stars 15 forks source link

Meteor Promise is unchainable if called from the server via Meteor.call #15

Open pyrostrex opened 8 years ago

pyrostrex commented 8 years ago

Test Method Code:

Meteor.methods({
    'test.method1': function() {
        return Meteor.call('test.method1b');
    },

    'test.method1b': function() {
        return Promise
            .resolve()
            .then(() => {
                return new Promise((resolve, reject) => {
                    setTimeout(() => {
                        resolve('hello');
                    }, 5000);
                })
            })
            .then((res) => {
                return `${res} world`;
            }).await();
    },

    'test.method2': function() {
        return Meteor.call('test.method2b');
    },

    'test.method2b': function() {
        return Promise
            .resolve()
            .then(() => {
                return new Promise((resolve, reject) => {
                    setTimeout(() => {
                        resolve('hello');
                    }, 5000);
                })
            })
            .then((res) => {
                return `${res} world`;
            });
    }
})

Results

Meteor.call('test.method1', (err, res) => console.log(res))
hello world [After 5 seconds]

Meteor.call('test.method2', (err, res) => console.log(res))
Object {} [Instantly]

Meteor.call('test.method2b', (err, res) => console.log(res))
Object {} [Instantly]

Meteor.call('test.method2c', (err, res) => console.log(res))
hello world [After 5 seconds]

Am I doing something wrong here?

test.method2 and test.method2b is calling test.method2c in the server and instantly returns Object {}. If I tried calling test.method2c directly from the client, it shows the correct output hello world.

Meanwhile, if I used .await() on the called method, it shows the correct output as demonstrate in test.method1 and test.method1b.

Note that the called function have a promise that delayed for 5 seconds.

I do apologize if it is because of my own mistakes.