Skip to content

Instantly share code, notes, and snippets.

@myfreeer
Last active December 5, 2016 13:21
Show Gist options
  • Save myfreeer/019ce116d241a0ec640db0f412e2c741 to your computer and use it in GitHub Desktop.
Save myfreeer/019ce116d241a0ec640db0f412e2c741 to your computer and use it in GitHub Desktop.
resolve array one after another using Promise with timeout
var resolvePromiseArrayWait = function resolvePromiseArrayWait(array, myPromise) {
'use strict';
var timeout = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
var retries = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
return new Promise(function (resolve, reject) {
var resultArray = [];
var resolver = function resolver(index) {
return setTimeout(function () {
return myResolver(index);
}, timeout);
};
var fails = function fails(index, e) {
return array[index + 1] ? console.warn('resolvePromiseArrayWait: index', index, 'failed! ', ', target:', array[index], ', error:', e, ',continue to next.', resolver(++index)) : console.warn('resolvePromiseArrayWait: index', index, 'failed! ', 'target:', array[index], ', error:', e, ', process done.', resolve(resultArray));
};
var myResolver = function myResolver(index) {
return myPromise(array[index]).then(function (result) {
return resultArray[index] = result;
}).then(function (e) {
return typeof array[++index] === "undefined" ? resolve(resultArray) : resolver(index);
}).catch(function (e) {
return retries-- > 0 ? resolver(index) : fails(index, e);
});
};
myResolver(0);
});
};
/* Examples below:
var myPromise = function myPromise(e) {
return new Promise(function (resolve, reject) {
return e % 2 ? console.log('resolve:', resolve(e), e) : console.log('reject:', reject(e), e);
});
};
var exampleArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
resolvePromiseArrayWait(exampleArray, myPromise, 300, 3).then(function (e) {
return console.log(e, 'done!');
});
*/
let resolvePromiseArrayWait = (array, myPromise, timeout = 0, retries = 0) => {
return new Promise((resolve, reject) => {
let resultArray = [];
let resolver = index => setTimeout(() => myResolver(index), timeout);
let fails = (index, e) => array[index + 1] ? console.warn('resolvePromiseArrayWait: index', index, 'failed! ', ', target:', array[index], ', error:', e, ',continue to next.', resolver(++index)) : console.warn('resolvePromiseArrayWait: index', index, 'failed! ', 'target:', array[index], ', error:', e, ', process done.', resolve(resultArray));
let myResolver = index => myPromise(array[index]).then(result => resultArray[index] = (result)).then(e => typeof (array[++index]) === "undefined" ? resolve(resultArray) : resolver(index)).catch(e => retries-- > 0 ? resolver(index) : fails(index, e));
myResolver(0);
});
};
/* Examples below:
let myPromise = e => new Promise((resolve, reject) => e % 2 ? console.log('resolve:', resolve(e), e) : console.log('reject:', reject(e), e));
let exampleArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
resolvePromiseArrayWait(exampleArray, myPromise, 300, 3).then(e => console.log(e, 'done!'));
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment