Code snippet:
// passed the url from an event stream, return a promise to load the document at the url
function loadXmlDoc(url) {
return Bacon.fromPromise($.ajax({url: url, dataType: xml}));
}
// create a stream that gets maps to a valid url when button is clicked.
var s1 = $('#load').asEventStream('click').map(getUrlToLoad);
// transform to a new stream that promises an ajax load of the document
var s2 = s1.flatMapLatest(loadXmlDoc);
// now do something with the document
s2.onValue(function(xml) { parseDoc(xml); } )
It's all working, but I'd like the parseDoc() function to also receive the url (location)
of the document that was loaded. But s2 is a promise that "evaluates" to
just the xml document. I'm afraid I'm new at promises and the jquery
ajax call. I guess there must be a way to rewrite loadXmlDoc so that the
promise that succeeds by returning an array or a dict, like:
{ data: xmlDoc, location: url }