javascript - Waiting for async request to return before proceeding Google Apps Script - Stack Overflow

Some code in Google Apps Script I am currently producing has a need for an object to be pleted with one

Some code in Google Apps Script I am currently producing has a need for an object to be pleted with one of the properties being set by the result of an async request. This object, and specifically, this property are then used later down the line. However, because the request has not returned by the time the functions needing that property run, they are not evaluating properly. My code is as follows:

function Thing(val) {
    var self = this

    var createSuccess = function(data) {
        self.foo = data;
    }

    var init = function(val) {
        google.script.run.withSuccessHandler(createSuccess).serverFunc(val);
    };

    init(val);
}

function objStuff() {
    var foobar = new Thing('bar');
    // Do stuff with foobar.foo
}

objStuff();

Currently the stuff using foobar.foo does not work correctly, as the script has not waited for the return value of the script before proceeding.

Is there a way I can wait for the foo property to be evaluated with the async request before proceeding with the rest of my script?

Some code in Google Apps Script I am currently producing has a need for an object to be pleted with one of the properties being set by the result of an async request. This object, and specifically, this property are then used later down the line. However, because the request has not returned by the time the functions needing that property run, they are not evaluating properly. My code is as follows:

function Thing(val) {
    var self = this

    var createSuccess = function(data) {
        self.foo = data;
    }

    var init = function(val) {
        google.script.run.withSuccessHandler(createSuccess).serverFunc(val);
    };

    init(val);
}

function objStuff() {
    var foobar = new Thing('bar');
    // Do stuff with foobar.foo
}

objStuff();

Currently the stuff using foobar.foo does not work correctly, as the script has not waited for the return value of the script before proceeding.

Is there a way I can wait for the foo property to be evaluated with the async request before proceeding with the rest of my script?

Share Improve this question edited Oct 3, 2021 at 23:43 Wicket 38.8k9 gold badges80 silver badges195 bronze badges asked Jun 4, 2017 at 21:55 dbrdbr 6891 gold badge8 silver badges22 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 6

You can add a callback as a parameter to the constructor:

function Thing(val, cb) {
    var self = this

    var createSuccess = function(data) {
        self.foo = data;
        cb(); // this gets called when data is ready
    }

    var init = function(val) {
        google.script.run.withSuccessHandler(createSuccess).serverFunc(val);
    };

    init(val);
}

function objStuff() {
    var foobar = new Thing('bar', function() {
        // Do stuff with foobar.foo
    });
}

objStuff();

My reendation would be to use the events module if you are making use of nodejs since it allows you to create an event listener.

So you could do something like

eventEmitter.on('listener_key', function(){
    //your code to be executed after the event
});

funtion emitStuffToListener(){
 eventEmitter.emit('listener_key');
}

EDIT: sorry I missread the post but you can still do event oriented development with Google Apps Event Objects

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745235798a4617885.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信