0
동시성 제한이 1 인 여기에 "지연 스트림 스트림에 매핑하는 방법"을 묻는 질문이 있습니다 : map a stream to lazy promise stream. 대답은 flatMapConcat
입니다.중첩 된 스트림이있는 flatMapConcat
이제 두 개의 중첩 된 스트림이 있는데, 동시성 제한이 1이어야하며 flatMapConcat
은 더 이상 작동하지 않습니다.
var getPromise = function(value) {
console.log('resolving ' + value);
return new RSVP.Promise(function(resolve, reject) {
setTimeout(function() {
resolve(value);
}, 1000);
});
};
var stream = Bacon.fromArray([1,2,3]).flatMapConcat(function(value) {
return Bacon.fromPromise(getPromise(value));
});
//stream.log();
// nice sequential
// resolving 1
// 1
// resolving 2
// 2
// resolving 3
// 3
var secondStream = stream.flatMapConcat(function(promised) {
return Bacon.fromPromise(getPromise('second' + promised));
});
secondStream.log();
// mixed up stream and secondStream, not sequential anymore
// resolving 1
// resolving second1
// resolving 2
// second1
// resolving second2
// resolving 3
// second2
// resolving second3
// second3
// Required output:
// resolving 1
// resolving second1
// second1
// resolving 2
// resolving second2
// second2
// resolving 3
// resolving second3
// second3
첫 번째 stream
좋은 순차적 없다,하지만 난 그것으로 중첩 된 스트림을 빌드 할 때, flatMapConcat
는 더 이상 작동합니다. 두 번째 스트림을 기다리는 첫 번째 스트림이 필요합니다.