Sometimes, we run into situations where I end up with a maybe within the context of another maybe. Nested structures like this can get really confusing to work with. In this lesson, we'll look at a example of this and see how can help us off by chain
flattening the nestedMaybe
As we all know, inside a array return another array is nested array.
Inside observable return another observable get observable of observable.
The same inside Just return another Just, we get Just of Just:
ConstProp = require ('Crocks/maybe/prop');ConstProppath = require ('Crocks/maybe/proppath');/** * Return maybe Type*/ConstGetUser = id = =NewPromise (Resolve, reject) = { Constresult ={status: $, body: {ID:1, Username:'Tester', Email:'[email protected]', Address: {street:'111 E. West St', City:'Anywhere', State:'PA', PostalCode:'19123-4567'}}} resolve (prop('Body', result));//return Just {} });ConstGetpostalcode =Proppath(['Address','PostalCode']); GetUser (1). Then (user= User.map (Getpostalcode))//map to Just {}--Just Just ' 19123-4567 '. then (Console.log);//Just Just "19123-4567"
Inside GetUser function We return a Just type Object. Then from Proppath, we get Just Just type Object. That's why we need flatten it.
The solve the problem is using flat map which are called ' chain ' in crocks.
GetUser (1) = user. Chain(getpostalcode). Option ("Notavailable")) // " 19123-4567 "
[Javascript Crocks] Flatten Nested maybes with ' chain '