Search code examples
javascriptjqueryajaxfrpbacon.js

How do you return an ajax response object using Bacon.js?


The following code from Bacon.js will push the data object returned from the AJAX request into the console.

   Bacon.fromPromise($.ajax({ url : requestUrlString })).log()

What is not clear, is how to assign the returned data object to a variable, or to do anything with the data object when a response is returned.


Solution

  • As per documentation (https://github.com/baconjs/bacon.js/#bacon-frompromise) the fromPromise method returns an EventStream object, whose onValue method you can use to attach a callback for handling the data. But that you can of course do without Bacon.js too. The point of the library is that you can gather, combine, filter and transform data from various sources and separate side-effects from data handling logic.