These are chat archives for jdubray/sam

5th
Oct 2016
Jean-Jacques Dubray
@jdubray
Oct 05 2016 00:33

apologies for such a basic question, not directly related to SAM, but I have been struggling to find an answer. I have to implement a function that must return a boolean:

filter: function(req,res) { 
   ...
   return true ;
}

However, I'd like to make an async call in that function which results will decide of the returned value.

Is there any way to wait on the results of that call? I have tried a number of things using promises, but nothing seems to work, similar questions have been asked on StackOverflow, but nothing seems to work.

My best guess would have been (assuming request was promisified):

filter: function(req,res) { 
   let val = request('http://google.com').then(function(res) { 
          if (...) return false;
    }) ; 
   return val ;
}

but that does not work.

Nivani
@Nivani
Oct 05 2016 07:19
If I understand correctly, you want to wait for an async result before returning? I don't know if there is a solution for that. Your bottom code snippet would be the correct way to handle a case like this, but it will return a promise. So you will be stuck with promises for the rest of your calculations.
Jean-Jacques Dubray
@jdubray
Oct 05 2016 07:33
yes this is where I ended up. I just wanted to double check. Thank you!
Alessandro Riva
@AlexRvTheWiz_twitter
Oct 05 2016 07:49
Frederik Krautwald
@Frikki
Oct 05 2016 08:29
Make your side-effectful function an observable stream. That is the solution
Jean-Jacques Dubray
@jdubray
Oct 05 2016 09:06
I tried the async library but I landed in the same place. @Frikki do you have a link?
Frederik Krautwald
@Frikki
Oct 05 2016 09:56
@jdubray A link on how to use observable streams? Check ReactiveX or most.js
Vincent Jo
@inrix-vincent-jo
Oct 05 2016 17:14
Hey JJ, it's been a while since I posted here but I want to try to help you here since I've been working a lot with Promises recently and experimented with the code snippet you posted up just now on repl.it
So yea like nivani said, the second code snippet should work, though the function that calls the filter() will have to use filter().then or filter().catch because when you return a value inside of .then() it will return a Promise
If you can, I would utilize Async/Await
It factors out dumb things about Promises and makes doing async so much easier... and you can avoid nested Promises which actually do happen believe it or not
I believe if you are already using Babel, this set up should be really easy.. I think it is just a matter of adding plugins .. 2 of them that worked for me were "transform-runtime" and "transform-async-to-generator"...
Vincent Jo
@inrix-vincent-jo
Oct 05 2016 17:22
but your case might a little different because I wrote TypeScript in ES7 (async/await feature) and the Typescript compiled it down to ES6 which then I used Babel to compile down to ES5

so with Asyn/Await, the code will look like

filter: async function(req,res) { 
   let val = await request('http://google.com');
   if (val === 2000) {
      return false;
   }
}

and on the other side

async function thatUsesFilter() {
   let final = await filter();
   // whatever here
   return final;
}
Vincent Jo
@inrix-vincent-jo
Oct 05 2016 17:27
One thing to note though about every async function is that it always returns a promise
so the 'final' and 'false' values are Promisified at Babel transpilation