-
Notifications
You must be signed in to change notification settings - Fork 0
/
movie.js
45 lines (40 loc) · 1.16 KB
/
movie.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// 获取今日预定的用户id列表
function getTodayUsers(callback) {
setTimeout(() => {
const users = [1, 2, 3, 4, 5];
callback(users);
}, 1000);
}
// 获取今日推荐的电影id
function getTodayMovie(callback) {
setTimeout(() => {
const movieId = 123;
callback(movieId);
}, 1000);
}
// 使用用户列表预定电影
function bookTodayMovieForUsers(userIds, movieId, callback) {
setTimeout(() => {
callback('订阅成功');
}, 1000);
}
// 为今天预定的用户订阅今日推荐的电影
function bookTodayMovieForTodayUsers() {
const usersPromise = promisify(getTodayUsers);
const moviePromise = promisify(getTodayMovie);
const bookTodayMovieForUsersPromise = promisify(bookTodayMovieForUsers);
return Promise.all([usersPromise(), moviePromise()]).then(args => {
const [userIds, movieId] = args;
return bookTodayMovieForUsersPromise(userIds, movieId);
});
}
function promisify(asyncFunc) {
return function promisied(...args) {
return new Promise((resolve, reject) => {
asyncFunc(...args, arg => {
resolve(arg);
});
});
};
}
bookTodayMovieForTodayUsers().then(msg => console.log(msg));