Quick actions

cmd+k|ctrl+k

Navigation

Languages

Promise All getDefaultParam

Snippet info

Language

JavaScript

Visibility

public

Author

horngyih

Created

2017-11-07T07:41:27Z

Updated

2017-11-07T07:41:50Z

function getItem(key){
    return new Promise(function(resolve, reject){
        if( key === "property" ){
            var delay = Math.random() * 10000;
            console.log( "Property Code Delay ", delay );
            setTimeout(function(){ resolve("PropertyCode");}, delay );
        } else if( key === "customer" ){
            var delay = Math.random() * 5000;
            console.log( "Customer Code Delay ", delay );
            setTimeout(function(){ resolve("customerCode");}, delay );
        }
    });
}

function getDefaultParams(){
    return new Promise(function(resolve, reject){
        var result = {
            propertyCode : null,
            customerCode : null
        };
        
        var chain = [ getItem("property"), getItem("customer")];
        Promise.all(chain).then(function(values){
            result.propertyCode = values[0];
            result.customerCode = values[1];
            resolve(result);
        });
    });
}

function get(){
    getDefaultParams().then(function(param){ 
        console.log( "Construct URL" );
        console.log(param); 
        console.log( "Call Get with ", param );
    });
}

get();
INFO