[Javascript Crocks] Flatten Nested maybes with ' chain '

Source: Internet
Author: User

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 '

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.