Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch value from Promise object after promise has been resolved

Please note This is a contrived example.

    function longFunc(){
        var deferred = $.Deferred();

        setTimeout(function(){
            console.log("long func completed");
            deferred.resolve("hello");
        }, 3000);

        return deferred.promise();
    }

    function shortAfterLongFunc(x){
        console.log('short func completed with value: ' + x);
        return {
            a: x
        };
    }

processFurther(longFunc().then(shortAfterLongFunc)); // send the array for further processing

Problem

I am unable to figure out how to return any kind of object/function for further downstream processing after shortAfterLongFunc completes. I can console.log from shortAfterLongFunc but that's not what i require here. Fiddle Here

Thanks for looking!

UPDATE:

Okay just to make my question slightly better...this is a simple use case I am looking at:

$.map(['H','E','L','L', 'O'], somefunc). // for each item in array apply somefunc function

function somefunc(x){ // gets called for each value 'H', 'E' etc. in the array by $.map()
    var longfunc = function(y){
        var deferred = $.Deferred();

        setTimeout(function(){
            console.log("long func completed");
            deferred.resolve(y.toLocaleLowerCase());
        }, 3000);

        return deferred.promise();
    };

    var shortAfterLongFunc = function(x){
        console.log('short func completed with value: ' + x);
        return x;
    }

    // What should I do here
    return longFunc(x).then(shortAfterLongFunc); // must return lower case char to the caller of someFunc

}

somefunc() lets say processes each element of Array to lower case. However, assume this processing takes a long time and async (think setTimeout).. hence a promise to ensure synchronous operation for each element...but on using promise I find myself not able return the transformed value

like image 732
Vikram Avatar asked Dec 18 '14 15:12

Vikram


People also ask

How do I return after promise is resolved?

resolve() method in JS returns a Promise object that is resolved with a given value. Any of the three things can happened: If the value is a promise then promise is returned. If the value has a “then” attached to the promise, then the returned promise will follow that “then” to till the final state.

Which method returns a promise object that is resolved with a given value?

resolve() method returns a Promise object that is resolved with the given value. It can be used to convert “promise-like” objects to native Promise objects: If you pass a thenable (an object with a then() method) to Promise. resolve() , the returned Promise object will eventually adopt the same state.

What happens when a promise is resolved?

A Promise that is resolved with the given value, or the promise passed as value, if the value was a promise object. It may be either fulfilled or rejected — for example, resolving a rejected promise will still result in a rejected promise.


1 Answers

Just chain another then call, since shortAfterLongFunc returns new promise you can further work with it:

longFunc().then(shortAfterLongFunc).then(function(data) {
    console.log('all is complted', data);
});

Demo: http://jsfiddle.net/ebt4pxxa/2/

like image 81
dfsq Avatar answered Sep 28 '22 18:09

dfsq