javascript - Clean up nested RxJS observable subscriptions -
question:
in angular 2 app, there few layers of nested subscriptions rest api calls return observables. what's best way remove nesting?
background:
i need calculate similarity between job's skills , user's skills jobs. need save similarity percentage corresponding match document. there's one-to-many relationship between users , matches. also, there's one-to-many relationship between jobs , matches. update match need find id first.
code:
// makes request '/api/jobs' this.jobservice.getjobs().subscribe((jobs: job[]) => { (let job of jobs) { // determine similarity between job's skills , user's skills let percentage = this.calculatepercentage(job.skills, this.currentuser.skills) // makes request '/api/matches/?jobid=12345&userid=11111' this.matchservice.getmatchforuserandjob(job._id, this.currentuser._id).subscribe((match: match) => { // makes patch request '/api/matches/54321' this.matchservice.updatematchpercentage(match._id, percentage).subscribe() }) } })
example schemas:
job
{ '_id': 12345, 'skills': [ { 'name': 'skill 1' }, { 'name': 'skill 2' } ] }
match
{ '_id': 54321, 'jobid': 12345, 'userid': 11111, 'percentage': 100 }
further questions:
is flatmap work in case?
is there better way update matches?
Comments
Post a Comment