-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path11-promises-errors.js
70 lines (65 loc) · 1.54 KB
/
11-promises-errors.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
var puts = require('./lib/puts')
var gets = require('./lib/gets')
function requestName () {
return new Promise(function (resolve, reject) {
puts('Enter Your Name: ')
gets(function (name) {
if (name === '') {
reject(Error('Your response cannot be empty'))
}
resolve(name)
})
})
}
function requestEmail () {
return new Promise(function (resolve, reject) {
puts('Enter Your Email: ')
gets(function (email) {
resolve(email)
})
})
}
function requestPhone () {
return new Promise(function (resolve, reject) {
puts('Enter Your Phone: ')
gets(function (phone) {
resolve(phone)
})
})
}
function requestBirthDate () {
return new Promise(function (resolve, reject) {
puts('Enter Your Birth Date: ')
gets(function (phone) {
resolve(phone)
})
})
}
requestName() // a Promise (which has a then method)
.then(function (name) {
puts('Name: ' + name)
}) // then method always return a new Promise
.catch(function (err) {
puts('Name Error Caught: ', err.message)
})
.then(requestEmail)
.then(function (email) {
puts('Email: ' + email)
})
.catch(function (err) {
puts('Email Error Caught: ', err.message)
})
.then(requestPhone)
.then(function (phone) {
puts('Phone: ' + phone)
})
.catch(function (err) {
puts('Phone Error Caught: ', err.message)
})
.then(requestBirthDate)
.then(function (birthDate) {
puts('Birth Date: ' + birthDate)
})
.catch(function (err) {
puts('Birth Error Caught: ', err.message)
})