-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpromise_example.js
52 lines (45 loc) · 925 Bytes
/
promise_example.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
var error = true;
var person = new Promise((resolve, reject) => {
setTimeout(() => {
if (error) {
return reject('error happend');
}
resolve({
name: 'SOME',
age: 30,
level: 999,
});
}, 100);
});
person
.then((man) => {
console.log('Welcome to 2019', man);
return man.name + ' say: hi, '+ 2019;
})
.then((data) => {
console.log(data);
return person;
})
.catch((error) => {
console.warn(error);
});
// var person1 = Promise.resolve('I am ONE');
// var person2 = new Promise((resolve, reject) => {
// setTimeout(() => {
// resolve('I am TWO');
// }, 300);
// }); ;
// var person3 = new Promise((resolve, reject) => {
// setTimeout(() => {
// resolve('I am THREE');
// }, 100);
// });
// Promise.all([
// person1,
// person2,
// person3
// ])
// .then(values => {
// console.log(values);
// // [ 'I am ONE', 'I am TWO', 'I am THREE' ]
// });