Вы помещаете результат вызовов get
в массив результатов асинхронно - res
пуст, когда вы вызываете Promise.all
для него:
arr.get(counter, item => {
res[counter] = function() {
return new Promise((resolve, reject) => {
return resolve(fn(item));
});
}();
});
// at this point, nothing has been pushed to res
Promise.all(res)
Вызывается синхронно после того, как эта точка ничего не будет ждать, потому что ни один из элементов в массиве не является Обещанием.
Вы можете вместо этого вставить Promise в массив синхронно :
res.push(new Promise((resolve) => {
arr.get(counter, item => resolve(fn(item)));
}));
/**
* Async array.
*/
function AsyncArray(arr) {
this._arr = arr;
this.length = arr.length;
}
/**
* Asynchronously get the array item of the
* given index.
* @param {number} index - array index of the desired item
* @param {function} callback - called with the array item
*/
AsyncArray.prototype.get = function get(index, callback) {
setTimeout(callback, 0, this._arr[index]);
};
/**
* Async version of Array.prototype.map.
* @param {AsyncArray} arr
* @param {function} fn - (item: any) => any
* @returns {Promise<AsyncArray>}
*/
function asyncMap(arr, fn) {
let counter = 0; // counter
const res = []; /// array of promises.
const len = arr.length;
// Get the length.
return new Promise((resolve, reject) => { // Pending.
while(true) {
if(counter===len) {
console.log("before break", res);
break;
}
res.push(new Promise((resolve) => {
arr.get(counter, item => resolve(fn(item)));
}));
counter += 1;
}
Promise.all(res).then((r1, rej) => {
console.log("hello world", r1);
return resolve(r1);
});
});
}
/**
* Async version of Array.prototype.reduce.
* @param {AsyncArray} arr
* @param {function} fn - (val: any, item: any) => any
* @returns {Promise<any>}
*/
function asyncReduce(arr, fn, initVal) {}
const arr = new AsyncArray([1, 2, 3]);
// arr.get(1, item => console.log(item)); // Existing
// Expected result: [2, 4, 6];
asyncMap(arr, x => x * 2).then(arr_ => console.log('asyncMap:', arr_));
// Expected result: 106
// asyncReduce(arr, (v, x) => v + x, 100).then(val => console.log('asyncReduce:', val));
Я бы предпочел использовать .map
в массиве длины внутреннего массива для сопоставления каждого arr.get
вызова с Promise в массиве и вызова Promise.all
в этом массиве:
function AsyncArray(arr) {
this._arr = arr;
this.length = arr.length;
}
AsyncArray.prototype.get = function get(index, callback) {
setTimeout(callback, 0, this._arr[index]);
};
function asyncMap(asyncArr, fn) {
return Promise.all(
new Array(asyncArr.length).fill().map(
(item, i) => new Promise(resolve => asyncArr.get(i, item => resolve(fn(item))))
)
).then((result) => {
console.log("hello world", result);
return result;
});
}
const arr = new AsyncArray([1, 2, 3]);
asyncMap(arr, x => x * 2).then(arr_ => console.log('asyncMap:', arr_));
Для асинхронного уменьшения, пусть аккумулятор будет Promise, который разрешается в массив после того, как он был помещен в последнюю итерацию:
function asyncReduce(asyncArr, fn, initVal) {
return new Array(asyncArr.length).fill().reduce(
(a, _, i) => a.then(resultsArr => {
// feel free to use asynchronous operations here
return new Promise((resolve) => {
asyncArr.get(i, resultItem => {
resultsArr.push(fn(resultItem));
resolve(resultsArr);
});
});
}),
Promise.resolve(initVal)
);
}
function AsyncArray(arr) {
this._arr = arr;
this.length = arr.length;
}
AsyncArray.prototype.get = function get(index, callback) {
setTimeout(callback, 0, this._arr[index]);
};
const arr = new AsyncArray([1, 2, 3]);
asyncReduce(arr, x => x * 2, []).then(arr_ => console.log('asyncReduce:', arr_));
Чтобы также вернуть экземпляры asyncArray
, просто позвоните new AsyncArray
перед разрешением, например:
function asyncReduce(asyncArr, fn, initVal) {
return new Array(asyncArr.length).fill().reduce(
(a, _, i) => a.then(resultsArr => {
// feel free to use asynchronous operations here
return new Promise((resolve) => {
asyncArr.get(i, resultItem => {
resultsArr.push(fn(resultItem));
resolve(resultsArr);
});
});
}),
Promise.resolve(initVal)
)
.then((resultArr) => new AsyncArray(resultArr));
}
function AsyncArray(arr) {
this._arr = arr;
this.length = arr.length;
}
AsyncArray.prototype.get = function get(index, callback) {
setTimeout(callback, 0, this._arr[index]);
};
const arr = new AsyncArray([1, 2, 3]);
asyncReduce(arr, x => x * 2, []).then(arr_ => console.log('asyncReduce:', arr_));