Promise.all 또는 Promise.map (Bluebird) 배열의 실행을 시작할 수 있기를 원합니다. 그 배열은 여전히 채워져 있고 다음에 기다리고 있습니다. 배열 채우기가 완료되면 결과가 반환됩니다. 이것은로드하는 데 오랜 시간이 걸리거나 메모리 제약을 초래할 수있는 큰 데이터 세트를 다룰 때 유용합니다.채워지는 약속 배열 결과 기다리는 중
는 데이터 (사용 블루 버드의 Promise.map를)로드 할 MongoDB의 커서를 사용하여, 오늘날 어떻게 작동하는지예 : 나는 그것을 할 방법의
// This is a time-intensive operation
// and requires loading all values into
// memory before proceeding
const dataArray = await cursor.toArray();
// Start doing work after all objects are in memory
await Promise.map(dataArray, doSomeWork);
// done
예 작동합니다 :
const dataArray = [];
const minItems = N;
let promiseMap = null;
// Populate the data array one item at a time
while (await cursor.hasNext()) {
dataArray.push(await cursor.next());
if (!promiseMap && dataArray.length > minItems) {
// Start doing work once there is some data to
// work with and keep filling the array
promiseMap = Promise.map(dataArray, doSomeWork);
}
}
// Once done filling the array, wait for all promises to resolve
await promiseMap;
// done
이것이 가능합니까 아니면 실행을 계속하기 전에 약속의 정적 배열을 갖는 것이 어려운 요구 사항입니까?
할 수 있습니다 단순히'workedOnMap = cursor.map (doSomeWork); ', 각 항목을 사용할 수있게되는 즉시 작업 할 수있게합니까? –
본질적으로 스트림과 같은 비동기 반복을 설명합니다. 이것을 가능하게하는 제안이있다. (비동기와 생성자 함수가 혼합되어있다.) 그러나'all'과'map'은 지금까지 정적 인 iterables에서만 작동한다. – Bergi
@ Roamer-1888 이것은 Roamer-1888이 처음 시도한 것이지만 mongodb 드라이버의 cursor.map 메서드가 약속 한 배열을 반환하지 않는다는 의미입니다. http://mongodb.github.io/node- mongodb-native/2.1/api/Cursor.html # map – bmajz