Skip to content

Instantly share code, notes, and snippets.

@implicit-invocation
Created October 12, 2017 14:45
Show Gist options
  • Save implicit-invocation/e12049e0594f715388cefe59798e2a1f to your computer and use it in GitHub Desktop.
Save implicit-invocation/e12049e0594f715388cefe59798e2a1f to your computer and use it in GitHub Desktop.
// const each = (arr, iterator, cb) => {
// const results = [];
// let done = 0;
// let hasError = false;
// for (let i = 0; i < arr.length; i++) {
// const item = arr[i];
// iterator(item, (err, result) => {
// if (hasError) return;
// if (err) {
// cb(err);
// hasError = true;
// return;
// }
// results[i] = result;
// done++;
// if (done == arr.length) {
// cb(null, results);
// }
// });
// }
// };
// const eachSeries = (arr, iterator, cb) => {
// const results = [];
// let hasError = false;
// let index = 0;
// const cloneArr = arr;
// const next = () => {
// iterator(arr[index], (err, result) => {
// if (hasError) return;
// if (err) {
// cb(err);
// hasError = true;
// return;
// }
// if (result != undefined) {
// results.push(result);
// }
// if (index < arr.length - 1) {
// index++;
// next();
// } else {
// cb(null, results);
// }
// });
// };
// next(cloneArr, iterator, cb);
// };
// const getAccountIds = (userId, cb) => {
// setTimeout(() => cb(null, [1, 2, 3]), 100);
// };
// const getAccountDetail = (accountId, cb) => {
// setTimeout(() => cb(null, `Detail of account ${accountId}`), 1000);
// };
// const getAccountDetails = (userId, cb) => {
// getAccountIds(userId, (err, accountIds) => {
// if (err) {
// return cb(err);
// }
// eachSeries(
// accountIds,
// (accountId, cb) => {
// console.log(`Processing ${accountId}`);
// getAccountDetail(accountId, cb);
// },
// cb
// );
// });
// };
// getAccountDetails(1, (err, details) => console.log(err, details));
// function* getId() {
// let i = 0;
// while (true) {
// yield i++;
// }
// }
// const idGenerator = getId();
// console.log(idGenerator.next().value);
// console.log(idGenerator.next().value);
// console.log(idGenerator.next().value);
// console.log(idGenerator.next().value);
// console.log(idGenerator.next().value);
// console.log(idGenerator.next().value);
// console.log(idGenerator.next().value);
const co = genFunc => {
};
const getAccountIds = userId => {
return new Promise((resolve, reject) => {
setTimeout(() => resolve([1, 2, 3]), 100);
});
};
const getAccountDetail = accountId => {
return new Promise(resolve =>
setTimeout(() => cb(null, `Detail of account ${accountId}`), 100)
);
};
getAccountIds(1).then(ids => console.log(ids)).catch(err => console.log(err));
co(function*() {
const ids = yield getAccountIds(1);
const details = [];
for (let id of ids) {
const detail = yield getAccountDetail(id);
details.push(detail);
}
console.log(details);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment