diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-07-05 13:26:25 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-07-05 14:14:16 +0200 |
commit | 6fcd19ba737f1f5614a56c6925adb882dea43b8d (patch) | |
tree | 3365a96d82bc7f00ae504a568725c8e914150cf8 /server/models/video/tag.ts | |
parent | 5fe7e898316e18369c3e1aba307b55077adc7bfb (diff) | |
download | PeerTube-6fcd19ba737f1f5614a56c6925adb882dea43b8d.tar.gz PeerTube-6fcd19ba737f1f5614a56c6925adb882dea43b8d.tar.zst PeerTube-6fcd19ba737f1f5614a56c6925adb882dea43b8d.zip |
Move to promises
Closes https://github.com/Chocobozzz/PeerTube/issues/74
Diffstat (limited to 'server/models/video/tag.ts')
-rw-r--r-- | server/models/video/tag.ts | 24 |
1 files changed, 8 insertions, 16 deletions
diff --git a/server/models/video/tag.ts b/server/models/video/tag.ts index 3c657d751..d0d8353d7 100644 --- a/server/models/video/tag.ts +++ b/server/models/video/tag.ts | |||
@@ -1,9 +1,8 @@ | |||
1 | import { each } from 'async' | ||
2 | import * as Sequelize from 'sequelize' | 1 | import * as Sequelize from 'sequelize' |
2 | import * as Promise from 'bluebird' | ||
3 | 3 | ||
4 | import { addMethodsToModel } from '../utils' | 4 | import { addMethodsToModel } from '../utils' |
5 | import { | 5 | import { |
6 | TagClass, | ||
7 | TagInstance, | 6 | TagInstance, |
8 | TagAttributes, | 7 | TagAttributes, |
9 | 8 | ||
@@ -52,10 +51,9 @@ function associate (models) { | |||
52 | }) | 51 | }) |
53 | } | 52 | } |
54 | 53 | ||
55 | findOrCreateTags = function (tags: string[], transaction: Sequelize.Transaction, callback: TagMethods.FindOrCreateTagsCallback) { | 54 | findOrCreateTags = function (tags: string[], transaction: Sequelize.Transaction) { |
56 | const tagInstances = [] | 55 | const tasks: Promise<TagInstance>[] = [] |
57 | 56 | tags.forEach(tag => { | |
58 | each<string, Error>(tags, function (tag, callbackEach) { | ||
59 | const query: any = { | 57 | const query: any = { |
60 | where: { | 58 | where: { |
61 | name: tag | 59 | name: tag |
@@ -67,15 +65,9 @@ findOrCreateTags = function (tags: string[], transaction: Sequelize.Transaction, | |||
67 | 65 | ||
68 | if (transaction) query.transaction = transaction | 66 | if (transaction) query.transaction = transaction |
69 | 67 | ||
70 | Tag.findOrCreate(query).asCallback(function (err, res) { | 68 | const promise = Tag.findOrCreate(query).then(([ tagInstance ]) => tagInstance) |
71 | if (err) return callbackEach(err) | 69 | tasks.push(promise) |
72 | |||
73 | // res = [ tag, isCreated ] | ||
74 | const tag = res[0] | ||
75 | tagInstances.push(tag) | ||
76 | return callbackEach() | ||
77 | }) | ||
78 | }, function (err) { | ||
79 | return callback(err, tagInstances) | ||
80 | }) | 70 | }) |
71 | |||
72 | return Promise.all(tasks) | ||
81 | } | 73 | } |