Mark Hollas
Mark Hollas

Reputation: 1137

jQuery execute function after complete

This is probably very basic but I can not seem to find a clear answer to my case.

I would like to know how to ensure that one function is executed and complete before the rest of the work flow can continue

in my script below the alerts fire as "1", "3", "2" where I would like getsS3policy(file) to be complete before the script steps into up.settings.multipart_params

preinit: {

            UploadFile: function (up, file) {
                getS3Policy(file);
                alert("3");
                    up.settings.multipart_params = {
                        key: path,
                        filename: file.name,
                        AWSAccessKeyId: 'AKIAJIRIA6XD4V5MFWCA',
                        acl: 'private',
                        policy: policy,
                        signature: signature,
                        success_action_status: '201'
                    }

            }
        },

    });
    function getS3Policy(file) {
        alert("1");
        data = { 'alc': 'private', 'bucket': 'PhotojimaDev', 'key': path, 'file': file.name };
        $.ajax({
            url: '/desktopmodules/uploader/API/upload/getPolicy',
            type: 'POST',
            data: data,
            success: function (response) {
                console.log(response);
                if (response) {
                    policy = response.policy;
                    signature = response.signature;
                    alert("2");
                }
                else {
                    return "Error";
                }
            }
        });

Upvotes: 0

Views: 158

Answers (2)

idrumgood
idrumgood

Reputation: 4924

The success function of your ajax call is the callback. That's what runs once the data is received if you put your up.settings.multipart_params in that callback, it will only run once the data is received.

$.ajax({
        url: '/desktopmodules/uploader/API/upload/getPolicy',
        type: 'POST',
        data: data,
        success: function (response) {
            console.log(response);
            if (response) {
                policy = response.policy;
                signature = response.signature;
                alert("2");
                up.settings.multipart_params = {
                    key: path,
                    filename: file.name,
                    AWSAccessKeyId: 'AKIAJIRIA6XD4V5MFWCA',
                    acl: 'private',
                    policy: policy,
                    signature: signature,
                    success_action_status: '201'
                }
            }
            else {
                return "Error";
            }
        }
    });

Upvotes: 1

Mooseman
Mooseman

Reputation: 18891

You're looking for a callback of jQuery ajax. See http://api.jquery.com/jQuery.ajax/

Upvotes: 1

Related Questions