Skip to content

Instantly share code, notes, and snippets.

@coderfin
Last active August 1, 2017 17:13
Show Gist options
  • Save coderfin/0ad5088a001f0e2703b2e224f7880874 to your computer and use it in GitHub Desktop.
Save coderfin/0ad5088a001f0e2703b2e224f7880874 to your computer and use it in GitHub Desktop.
Recursive Promise
{
const readAllRecordsAsync = ({ params }) => {
return new Promise((resolve, reject) => {
resolve(scanAsync({ params, records: [] }));
});
};
const scanAsync = ({ params, records }) => {
records.push(params.n);
if (params.n === 0) {
return records;
}
return new Promise((resolve, reject) => {
scan(params, (n) => {
params.n = n;
resolve(scanAsync({ params, records }));
});
});
};
const scan = (params, callback) => {
callback(params.n - 1);
};
readAllRecordsAsync({
params: {
n: 3
}
})
.then((finalRecords) => {
console.log(finalRecords);
})
.catch(error => {
console.error(error);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment