Last active
August 1, 2017 17:13
-
-
Save coderfin/0ad5088a001f0e2703b2e224f7880874 to your computer and use it in GitHub Desktop.
Recursive Promise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
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