Linked Questions

Popular Questions

'filling' an object with async values

Asked by At

I have an object with multiple properties and a few of them contain promises. Now i want to wait until every promise is resolved and then the properties should contain the value of the promise

This is what i have so far, unfortunately the object o still contains the fullfilled promises on the properties o3, o4, o5:

var f_o_data = function(b_reject){

    return new Promise(
        function(
            f_resolve, 
            f_reject
        ){

            window.setTimeout(function(){

                var n_ts_ms = parseInt(new Date().getTime());
                // if(n_ts_ms % 2 == 0){
                //     f_reject(`i rejected because n_ts_ms ${n_ts_ms} % 2 == 0 is true :shrug:`)
                // }
                if(b_reject){
                    f_reject('i rejected because true was passed to function')
                }
                f_resolve(
                    {
                        n_ts_ms: n_ts_ms,
                        s: 'i am the result object', 
                        n: 23, 
                        b: true, 
                        a_n: [1,2,3], 
                        a_o:[{n:2, n:3}, {n:3}]
                    }
                )

            },parseInt(Math.random()*3333+555))
        }
    )
}
var f_o__filled_asyncally = async function(){

    var o = {
        b: true, 
        n: 2, 
        s: 'just a string', 
        o: {n:22}, 
        a_n: [1,2,3],
        a_o:[{n:2, n:3}, {n:3}],
        o1: await f_o_data(),// those two first async functions will be executed one after another because we use await keyword
        o2: await f_o_data(),// 
        o3: f_o_data(),// theese three can load parallel
        o4: f_o_data().then(function(o){return o}),// theese three can load parallel
        o5: f_o_data(),// theese three can load parallel
        // o6: f_o_data(true),// theese three can load parallel
    }
    return Promise.all(Object.values(o)).then(
        function(a_o_promise){
            return o
        }
    )
    // console.log(o)
    // console.log(o.o3)
    // console.log(o.o4)
    // console.log(o.o5)
    // return o;
}

var o_my_object_i_want_to_fill_asyncally = await f_o__filled_asyncally();
console.log(o_my_object_i_want_to_fill_asyncally)

Related Questions