开发者

Passing parameters between javascript asynchronous callback functions

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜