Skip to content

Commit 82d2be1

Browse files
authored
Merge pull request popcorn-official#2822 from popcorn-time-ru/q-to-promise
2 parents e97c118 + 83a948e commit 82d2be1

File tree

1 file changed

+17
-64
lines changed

1 file changed

+17
-64
lines changed

src/app/lib/providers/favorites.js

+17-64
Original file line numberDiff line numberDiff line change
@@ -144,24 +144,17 @@
144144
movie_fetch_func = movie_fetch_wrapper;
145145
show_fetch_func = show_fetch_wrapper;
146146
}
147-
var deferred = Q.defer();
148147
// we check if its a movie
149148
// or tv show then we extract right data
150149
if (movie.type === 'movie') {
151150
// its a movie
152-
movie_fetch_func(movie.imdb_id)
153-
.then(function (data) {
154-
if (data && shouldMark) {
155-
data.type = 'bookmarkedmovie';
156-
}
157-
if (data && /slurm.trakt.us/.test(data.image)) {
158-
data.image = data.image.replace(/slurm.trakt.us/, 'walter.trakt.us');
159-
}
160-
deferred.resolve(data);
161-
},
162-
function (err) {
163-
deferred.reject(err);
164-
});
151+
const promise = movie_fetch_func(movie.imdb_id).then(function (data) {
152+
if (data && shouldMark) {
153+
data.type = 'bookmarkedmovie';
154+
}
155+
return data;
156+
});
157+
movieList.push(promise);
165158
} else {
166159
// its a tv show
167160
if (kind === 'Watched') {
@@ -171,59 +164,19 @@
171164
}
172165
WseriesList.push(movie.imdb_id);
173166
}
174-
var _data = null;
175-
show_fetch_func(movie.imdb_id)
176-
.then(function (data) {
177-
if (data && shouldMark) {
178-
data.type = 'bookmarkedshow';
179-
}
180-
data ? data.imdb = data.imdb_id : null;
181-
// Fallback for old bookmarks without provider in database or marked as Eztv
182-
if (data && typeof (data.provider) === 'undefined' || data.provider === 'Eztv') {
183-
data.provider = 'tvshow';
184-
}
185-
// This is an old boxart, fetch the latest boxart
186-
if (data && /slurm.trakt.us/.test(data.images.poster)) {
187-
// Keep reference to old data in case of error
188-
_data = data;
189-
var provider = App.Providers.get(data.provider);
190-
return provider.detail(data.imdb_id, data);
191-
} else {
192-
data ? data.poster = data.images.poster : null;
193-
deferred.resolve(data);
194-
return null;
195-
}
196-
}, function (err) {
197-
deferred.reject(err);
198-
}).then(function (data) {
199-
if (data) {
200-
if (shouldMark) {
201-
// Cache new show and return
202-
Database.deleteBookmark(_data.imdb_id);
203-
Database.deleteTVShow(_data.imdb_id);
204-
Database.addTVShow(data);
205-
Database.addBookmark(data.imdb_id, 'tvshow');
206-
data.type = 'bookmarkedshow';
207-
}
208-
deferred.resolve(data);
209-
}
210-
}, function (err) {
211-
// Show no longer exists on provider
212-
// Scrub bookmark and TV show
213-
// But return previous data one last time
214-
// So error to erase database doesn't show
215-
if (_data && shouldMark) {
216-
Database.deleteBookmark(_data.imdb_id);
217-
Database.deleteTVShow(_data.imdb_id);
218-
}
219-
deferred.resolve(_data);
220-
});
167+
const promise = show_fetch_func(movie.imdb_id).then(function (data) {
168+
if (data && shouldMark) {
169+
data.type = 'bookmarkedshow';
170+
}
171+
data ? data.imdb = data.imdb_id : null;
172+
data ? data.poster = data.images.poster : null;
173+
return data;
174+
});
175+
movieList.push(promise);
221176
}
222-
223-
movieList.push(deferred.promise);
224177
});
225178

226-
return Q.all(movieList).then(values => values.filter(v => v));
179+
return Promise.all(movieList).then(values => values.filter(v => v));
227180
};
228181

229182
Favorites.prototype.extractIds = function (items) {

0 commit comments

Comments
 (0)