willconant / flow-js

Javascript Library for Multi-step Asynchronous Logic
MIT License
302 stars 15 forks source link

More complex serialForEach example #10

Open scottsd opened 10 years ago

scottsd commented 10 years ago

Hello,

I'm trying to put together a more complex serialForEach example, which includes "jumping" manually to next step, based on the example in issue #3. step 1: search for the value step 2: if found, just go to next step. If not, search again using different parameters, and go to next step using that value


flow.serialForEach(["refe", "lkajdflkjwernm", "crypto", "poidfgmnermn"], function(val) {
  console.log("Searching for " + val)
      $.ajax({
                type: "GET",
        url: 'https://api.stackexchange.com/2.1/badges?key=U4DMV*8nvpm3EOpvf69Rxw((&site=stackoverflow&order=desc&sort=rank&filter=default',
        data: {inname: val},
                success: this
      })

},function(search_result) {
  console.log('search_result')  
  console.log(search_result)

  var next = this
  if (search_result.items.length > 0) {
    console.log('found')  
    next(search_result.items[0])
  } else {
    console.log('NOT found')  
    //just get the latest badge
      $.ajax({
                type: "GET",
        url: 'https://api.stackexchange.com/2.1/badges?key=U4DMV*8nvpm3EOpvf69Rxw((&site=stackoverflow&order=desc&sort=rank&filter=default',
        data: {},
                success: this
      })

  }

},function(badge) {
  console.log('badge')  
  console.log(badge)  

},function() {
    console.log('This is the end!');
});

There are multiple issues with this sample:

  1. the 'badge' step isn't called with additional data passed as parameters. 'badge' is always undefined.
  2. processing stops after 2nd value in serialForEach array
  3. 'This is the end!' never makes it

How should the code look for this to work?

Thanks