I'm using RxJS 5.0.0-alpha.12 to perform some data transformations, and some of those transformations use Promises.
return observable
.flatMap(item => {
return Observable.zip(
Observable.fromPromise(fs.readFile('./config.json')),
Observable.of(item),
(s1, s2) => {
return {
config: JSON.parse(s1.toString()),
testResult: s2
};
}
);
})
.flatMap(item => {
return Observable.zip(
Observable.fromPromise(
getStudentIdFromStudentNumber(item.testResult['Student Primary ID'])
),
Observable.of(item),
(s1, s2) => {
return {
'Test Date': s2.config.testConstants.ROGL_Begin_Year.testDate,
'Student Id': s1,
'Student Number': s2.testResult['Student Primary ID'],
'Grade Level': s2.testResult.Grade,
'Composite Score Alpha': s2.testResult['Composite Score']
};
}
);
});
Note that both fs.readFile
(fs
here being the name given to fs-promise) and getStudentIdFromStudentNumber
return a Promise.
It seems like having to zip
the original item
that is getting emitted from observable
with the results of each Promise is cumbersome. Is there a cleaner way to use the results of these two Promises with this Observable?
What makes this tricky (to me) is sometimes the Promise relies on a field within the item
object emitted by observable
(eg., getStudentIdFromStudentNumber
), and other Promises may not rely on item
(eg., fs.readFile
).