0

I am building a FB bot and am trying to find a way to segment my answers to certain user inputs.

As of right now I have a large switch-case of over 600 entries but was wondering if it would be possible to segment it into several smaller lists in which I could run a single Promise to run through all async.

Here's what I have considered and already attempted:

  1. 1 switch-case: works, but seems awkward as the cases vary vastly by subject. i.e conversational cases ("hi, hey! etc) and certain requests for restaurants or events.
  2. Hashtable lookup - same problem as above - seems clunky and awkward to mix different subjects.
  3. Custom Promise that ignores rejects() and stops at first resolve. This worked for my segmented lists that were all Promisified but in regards to the reject case I was unable to get a response... I tried:

    var t1 = test.switchCase1(event); //switch case 1 - restaurants
    var t2 = test.switchCase2(event); //switch case 2 - conversation
    var t3 = test.switchCase3(event); //switch case 3 - misc
    
    //resolve first, ignore rejects
    if (!Promise.first) {
      Promise.first = function (promise) {
        return new Promise ( (resolve,reject) => {
          promise.forEach( (prs) => {
            Promise.resolve(prs)
              .then(resolve);
          })
        })
      }
    }
    Promise.first([t1,t2,t3]).then(response => {
      utils.callSendAPI(response); //sends response to user
    });
    

Am I looking at this the wrong way? I've looked up tons of options such Object literal lookup but nothing seems to provide an answer to what I'm looking for.

  • See http://stackoverflow.com/a/31424853/ – guest271314 May 02 '17 at 06:33
  • Sounds like you are looking for `Promise.race`. – Bergi May 02 '17 at 07:58
  • What exactly do you think is clunky and awkward about hash tables? If it's just the notation, then ask about how to split it up in multiple files (one per subject). – Bergi May 02 '17 at 07:59
  • @Bergi Promise.race ends on first resolve or reject meaning several separated hashtables by subject would need some fulfillment or rejection to break out of them. The custom promise above works like a race but only accepts the first resolve and ignores the rejects. Will edit title, thanks – GoingOutW3st May 02 '17 at 17:02
  • I don't really get why you would want to use promises anyway. There's nothing really asynchronous in here, is there? – Bergi May 02 '17 at 20:31

0 Answers0