简体   繁体   中英

2 javascript object event handlers interaction

I want to do the following

let o1
let o2

o1.call()
  .on('x', function(result1) {

    o2.call()
      .on('y', function(result2) {  }) //expecting a value returned 

    result1 += result2 //modify result1
  })
  .next(function(result1) { 
    // do something with modified result1      
  })

Above is just pseudocode.

I want to modify result1 inside on('x'), does it wait for o2.on('y')?

How can I modify result1 with result2 in on('x')?

Effectively I want something like the following:

let o1
let o2

o1.call()
  .on('x', function(result1) {

    result1 += o2() //sync, but I have no control over this o2 external library/ object that was designed to be async

  })
  .next(function(result1) { 
    // do something with modified result1      
  })

You can modify result1 inside function(result2) { } :

.on('y', function(result2) {
    result1 += result2
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM