diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2018-10-02 16:31:44 +0200 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2018-10-02 16:31:44 +0200 |
| commit | 2a17aa1331b1e7754a1a374ed285ac0e9def9af3 (patch) | |
| tree | c147b073b19fc3c9a0cde9fc40bb1c3ccd288cfc /bucky | |
| parent | 2b22142a823ca38475639993cfea5d3173065531 (diff) | |
more functional federate promises, push comments/files in series rather than async
Diffstat (limited to 'bucky')
| -rw-r--r-- | bucky/util/federate.js | 76 |
1 files changed, 39 insertions, 37 deletions
diff --git a/bucky/util/federate.js b/bucky/util/federate.js index b782801..5181d85 100644 --- a/bucky/util/federate.js +++ b/bucky/util/federate.js @@ -30,45 +30,44 @@ module.exports = { } function exportKeyword (req, res, next) { console.log('export keyword', req.params.keyword) - db.getKeyword(req.params.keyword).then(keyword => { - send("keyword", keyword) - return db.getThreadsForKeyword(req.params.keyword) - }).then(threads => { - var promises = threads.map(thread => { - exportThread({ params: { id: thread.get('id') } }, res, function(){}) - }) - return Promise.all(promises) - }).then( () => { - next() - }) + return db.getKeyword( + req.params.keyword + ).then(keyword => send("keyword", keyword) + ).then(res => db.getThreadsForKeyword(req.params.keyword) + ).then(threads => threads.reduce((promise, thread) => ( + promise.then(result => exportThread({ params: { id: thread.get('id') } }, res, function(){})) + ), new Promise (resolve => resolve())) + ).then(() => next()) } function exportThread (req, res, next) { var thread_id - return db.getThread(req.params.id).then(thread => { - return send("thread", thread) - }).then(json => { - thread_id = json.el.id - console.log('got thread id', thread_id) + return db.getThread( + req.params.id + + ).then(thread => send("thread", thread) + ).then(json => { + console.log('got thread id', json.el.id) return db.getCommentsForThread(req.params.id) - }).then(comments => { - var promises = comments.map(comment => { - comment.set('thread', thread_id) - send("comment", comment) - }) - return Promise.all(promises) - }).then( () => { - return db.getFilesForThread(req.params.id) - }).then(files => { - var promises = files.map(file => { - copyFileToS3(file, thread_id) - file.set('thread', thread_id) - file.set('storage', process.env.S3_BUCKET) - return send("file", file) - }) - return promises - }).then( () => { - next() - }).catch(e => { + + }).then(comments => comments.reduce((promise, comment) => ( + promise.then(result => { + comment.set('thread', thread_id) + return send("comment", comment) + }) + ), new Promise(resolve => resolve())) + + ).then(() => db.getFilesForThread(req.params.id) + ).then(files => files.reduce((promise, file) => ( + promise.then(result => { + copyFileToS3(file, thread_id) + file.set('thread', thread_id) + file.set('storage', process.env.S3_BUCKET) + return send("file", file) + }) + ), new Promise(resolve => resolve())) + + ).then(() => next() + ).catch(e => { console.error(e) next() }) @@ -83,8 +82,11 @@ module.exports = { 'Content-Type': 'application/json', 'Accept': 'application/json', }, - }).then((res) => {return res.json()}) - then((json) => console.log(json)) + }).then(res => res.json()) + .then(json => { + console.log(json) + return json + }) } function copyFileToS3(file, thread_id){ // since for now we are essentially backing up local files, |
