Revising the Main Function Using the JSONP Driver
Learn about the changes that need to be introduced in the main function when the JSONP driver is used.
We may have noticed in the code of the Basic Understanding of Cycle.js lesson that the main
function takes a parameter, responses
, that it does not use. These are the responses that come from the drivers
function in the run
function. The drivers and the main
function form a cycle (hence the name of the framework): the output of the main
function is the input of the drivers
function, and the output of the drivers
function is the input for the main
function. And remember, input and outputs are always Observables.
We use JSONP to query Wikipedia, as we did previously. We use JSONP instead of HTTP to make it easier to run this example on our local computers, since retrieving data from a different domain using HTTP causes some browsers to block those requests for security reasons. In almost any other situation, especially in production code, we use HTTP to retrieve remote data.
In any case, using JSONP doesn’t affect the point of this chapter. Cycle has an experimental module for JSONP, and we can install it using npm
:
npm install @cycle/jsonp
Then, we will use it in our application like this:
Get hands-on with 1200+ tech skills courses.