meteor / promise

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

Example #4

Open rootedsoftware opened 9 years ago

rootedsoftware commented 9 years ago

Please include an example of using this package in an app.

Thanks

Alino commented 9 years ago

+1

josephdburdick commented 9 years ago

Or just write it in a couple code samples of mock functions on client / server side.

estaub commented 8 years ago

+1

MilosStanic commented 8 years ago

+1

mrvictorn commented 8 years ago

+1

metalik commented 8 years ago

+1

lyricalpolymath commented 8 years ago

+1

Alino commented 8 years ago

check this out, I believe it works the same as ES6 promise, it is a polyfill after all https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise

lyricalpolymath commented 8 years ago

I played around with Promise and manage to get something working. for now only Promise.await() seems to be working. (as said in this other issue: async is not enabled yet)

Copy paste this into your meteor code and you'll understand better how to use it. try the 2 return types of the delayedFunction to really get the grasp of Promise.await

// you don't need to import Promise since it is is already a global
        const TAG = "promiseTest ";

        // SIMPLIFIED WORKING SYNTAX (only Promise.await works)
        var delayedFunction = function() {
            var p = new Promise( function(onResult, onReject) {

                //simulate the server lag with a setTimeout
                console.log(TAG + " 2 -------- INSIDE delayedFunction but BEFORE TIMEOUT");
                setTimeout( function() {
                    console.log(TAG + " 3 -------- INSIDE delayedFunction + AFTER TIMEOUT");

                    //this is the real function body you would have that calls the slow to answer process
                    // ... some slow to answer code
                    //and when it's done it calls the onResult() function passing it the value you are waiting from the promise
                    onResult("promised Value");
                    //or handle the error passing it to onReject()

                },4000)
            });

            //OPTIONAL - now that you have a promise you can put .then and do other stuff with the value
            // WARNING: these ".then" can perform other tasks WITH the "promised Value", BUT even if you try to modify it, it won't change the returned value of delayedFunction:
            // delayedFunction will always return "promised Value"
            p.then(    (result) => {
                            console.log(TAG + " 4a1 -------- THEN 1 From inside delayedFunction result: " + result);
                            result += " modified by the THEN1";
                            return result;
                    })
                .then( (result) => console.log(TAG + " 4a2 -------- THEN 2 From inside delayedFunction result: " + result))  // result is manipulated but it doesn't change the final value
                .then( (result) => console.log(TAG + " 4a3 -------- THEN 3 From inside delayedFunction result: " + result)); // result is undefined unless returned by the previous .then

            // this is executed immediately - it doesn't wait for all the return values - see the weird structure of a promise
            console.log(TAG + " 2b -------- INSIDE delayedFunction but AFTER TIMEOUT  p: ", p);

            // IF YOU DON'T HAVE ".then"
            // WARNING if you return the promise p, for some reason you can't use the .then in the main scope like you are doing here
            // p will actually be returning the desired value
            // my theory: Promises are generators (function*) so you are not returning the generator function but an interator with the specific value
            // return p;

            // OR if you have MULTIPLE ".then" values in here
            // put as many nested Promise.await here as there are ".then" (3 in this case)
            // in the main scope simply run the function like:
            // var prom = delayedFunction()
            return Promise.await(Promise.await(Promise.await(p)))
        }

        console.log(TAG + " 1 -------- BEFORE instantiation");
        // if delayedFunction has "return p"
        //var prom = Promise.await(delayedFunction());      //prom == "promised Value"

        //if delayedFunction returns one or multiple "Promise.await(p)"
        var prom = delayedFunction();                       //prom == "promised Value"
        console.log(TAG + " 5 -------- AFTER Promise.async prom: >>>>> ", prom);
lnmunhoz commented 8 years ago

Could this wraps all meteor methods calls in a promise like mongoose does?