Mr always wrong
Mr always wrong

Reputation: 299

Fire after function is done

Hey guys this is the situation

function makeIt()
{
  //code
  createSomething()
}

function createSomething(){
 //code
  request.execute(function(resp) {
  function writeSomething(){
    //code
  }
   createSomething()
   goback()
 }
}

I want to execute the goback after writesomething is done. The problem is that the writesomething function can be done in 2 sec or in 10 sec(depends on the file). I use for now a setTimeout just to be sure. But how can I let goback exucute when writesomething is done ?

EDIT:

function writeToSheet(){
//this is a part of the function (deleted some information)
                params 

            var xhr = new XMLHttpRequest();

            xhr.open('PUT', 'https://something.com');
            xhr.setRequestHeader(some things);
            xhr.send(JSON.stringify(params));
        }   

Upvotes: 0

Views: 82

Answers (2)

T.J. Crowder
T.J. Crowder

Reputation: 1074495

See update at the bottom, now that you've included the writeToSheet definition.


If writeSomething is an asynchronous process, it will provide a way for you to know when it's done — it'll accept a callback, return a promise, etc. Pass goback as that callback (or as the then callback on the promise, etc.).

Example - if writeSomething accepts a callback:

writeSomething(other, arguments, here, goback);
// This is the callback ---------------^^^^^^

or

writeSomething(other, arguments, here, function() {
    goback();
});

...depending on whether you want goback to receive whatever arguments writeSomething passes its callback.

Example - if writeSomething returns a promise:

writeSomething(other, arguments, here).then(goback);

or

writeSomething(other, arguments, here).then(function() {
    goback();
});

...again depending on whether you want goback to receive the value passed to the then callback.


If writeSomething is a synchronous process that may take 2-10 seconds, just call goback() after calling writeSomething. Even if writeSomething takes 10 seconds, if it's truly synchronous, goback won't be called until it's done.

Example (just for completeness :-) ):

writeSomething(other, arguments, here);
goback();

Update: Your writeToSheet function starts an asynchronous process, so we want to edit it to either accept a callback or return a promise.

Accepting a callback:

function writeToSheet(callback){
//                    ^------------------------------------ ***
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {                // ***
        if (xhr.readyState === 4) {                      // ***
            callback(xhr.status === 200);                // ***
        }                                                // ***
    };
    xhr.open('PUT', 'https://something.com');
    xhr.setRequestHeader(some things);
    xhr.send(JSON.stringify(params));
}

writeToSheet will call the callback with true if it was successful, false if not.

Then:

writeToSheet(goback);

or

writeToSheet(function(flag) {
    // Maybe use the flag here, or not, depends on what you want
    goback();
});

...if you don't want goback to receive the flag.

Returning a promise:

function writeToSheet(){
    return new Promise(function(resolve, reject) {           // ***
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function() {                // ***
            if (xhr.readyState === 4) {                      // ***
                if (xhr.status === 200) {                    // ***
                    resolve();                               // ***
                } else {                                     // ***
                    reject();                                // ***
                }                                            // ***
            }                                                // ***
        };
        xhr.open('PUT', 'https://something.com');
        xhr.setRequestHeader(some things);
        xhr.send(JSON.stringify(params));
    });
} 

then:

writeToSheet().then(goback).catch(function() {
    // It failed
});

...which will only call goback on success and call the other function on failure, or

writeToSheet().then(goback, goback);

...which will call goback regardless.

Upvotes: 3

jesuisgenial
jesuisgenial

Reputation: 725

pass your callback function as a parameter

function writeSomething(callback) {
    ...
    callback();
}

function myCallbackFunction() {
    alert('hi!');
}

writeSomething(myCallbackFunction);

Upvotes: 0

Related Questions