开发者

Passing parameters between javascript asynchronous callback functions

开发者 https://www.devze.com 2023-02-08 01:08 出处:网络
I want to use the execAsync function here: https://developer.mozilla.org/en/Storage#Asynchronously and I want to pass values between handleResult and handleCompletion. Something like

I want to use the execAsync function here: https://developer.mozilla.org/en/Storage#Asynchronously

and I want to pass values between handleResult and handleCompletion. Something like

statement.executeAsync({
  handleResult: function(aResultSet) {
    VALUE = 1
  },

 开发者_开发知识库 handleCompletion: function(aReason) {
    print(VALUE);
  }
});

What's the best way to do it?


var value;

statement.executeAsync({
  handleResult : function(aResultSet) {
    value = 1;
  },
  handleCompletion : function(aReason) {
    print(value);
  }
});


Well the obvious thing to notice is that you're passing an object to executeAsync. (In particular, it's a mozIStorageStatementCallback, so it should have a handleError method too.) So you can easily associate properties specific to that object with the object, using the "this" keyword:

statement.executeAsync({
  value: 1,
  handleResult: function(aResultSet) {
    this.value = 0;
  },
  handleError: function(aError) {
    this.value = 2;
  },
  handleCompletion: function(aReason) {
    print(this.value);
  }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消