0

I need to return a custom object that relies on multiple async jquery ajax calls.

Sample code below:

function BuildMyObject() {
    var bp = null;
    var accountId = getAccountId();
    var signature = getSignature();
    var transactionType = getTransactionType();
    var mode = getMode();
    var merchantIdentifier = getMerchantIdentifier();
    jQuery.when(accountId, signature, transactionType, mode, merchantIdentifier).done(function(rAcctId,
        rSig,
        rTransType,
        rMode,
        rMerchId) {
        bp = new MyObject(
            rAcctId[0],
            rSig[0],
            rTransType[0],
            rMode[0],
            rMerchId[0]);
    });
    return bp;
}

Now, this returns null; as the return statement is firing before the async calls finish. Is there a way to wait untill all the calls are finished before returning the object? If i set the ajax calls to synchronious, it works; but I'd like to avoid that.

1
  • 1
    Simply return jQuery.when(..) Dec 7, 2017 at 20:24

1 Answer 1

2

you can work with Promises or callbacks for scenarios of async functions

//callback example
function BuildMyObject(callback) {
    var bp = null;
    var accountId = getAccountId();
    var signature = getSignature();
    var transactionType = getTransactionType();
    var mode = getMode();
    var merchantIdentifier = getMerchantIdentifier();
    jQuery.when(accountId, signature, transactionType, mode, merchantIdentifier).done(function(rAcctId,
        rSig,
        rTransType,
        rMode,
        rMerchId) {
        bp = new MyObject(
            rAcctId[0],
            rSig[0],
            rTransType[0],
            rMode[0],
            rMerchId[0]);
            )
        callback(bp)
           
    });
   
}


//or with promise

function BuildMyObject() {
    return new Promise((resolve,reject) =>{
        var bp = null;
        var accountId = getAccountId();
        var signature = getSignature();
        var transactionType = getTransactionType();
        var mode = getMode();
        var merchantIdentifier = getMerchantIdentifier();
        jQuery.when(accountId, signature, transactionType, mode, merchantIdentifier).done(function(rAcctId,
            rSig,
            rTransType,
            rMode,
            rMerchId) {
            bp = new MyObject(
                rAcctId[0],
                rSig[0],
                rTransType[0],
                rMode[0],
                rMerchId[0]);
           )
           resolve(bp)
        });

    }) 
  
   
}

BuildMyObject()
.then((bp) =>{
    //do somthing with bp
})

2
  • For the promise example, you could just return bp in the .done callback, and return the chain. e.g. return jQuery.when(...).done(...) instead of wrapping the entire thing in a promise constructor.
    – caseyWebb
    Dec 7, 2017 at 20:29
  • @amit; the promise option looks like it could work; but how do i get the bp object back out? Right now it appears to be returning the promise object; when i really want the assembled bp object back
    – Jim B
    Dec 7, 2017 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.