repl.it
@vcttai/

Promise - Loi hua ngot ngao P1

ES6

No description

fork
loading
main.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
/* ES5 */
var isSheHappy = true;          //you can change it to FALSE to get the different result
 
// Promise
var willBeCouple = new Promise(
    function (resolve, reject) {
      if (isSheHappy) {
        var answer = {feedback: "I accept to be your girlfriend"};
        
        setTimeout(function(){
          resolve(answer); // fulfilled
        }, 1000);
      } 
      else {
        var reason = new Error('I do not love you');
        
        setTimeout(function(){
          reject(reason); // reject
        }, 1000);
      }
    }
);

// 2nd promise
var showOff = function (answer) {
    return new Promise(
      function (resolve, reject) {
        var message = 'Hey friend, she accepted. This is the result: ' + answer.feedback;
        resolve(message);
      }
    );
};


// call our promise
var askYourGirlFriend = function () {
    console.log("before promise!");
 
    willBeCouple
      .then(showOff) // chain it here
      .then(function (fulfilled) {
        console.log(fulfilled);
        // output: 'Hey friend, she accepted. This is the result: I accept to be your girlfriend.'
      })
      .catch(function (error) {
        // oops, she refused it
        console.log(error.message);
        // output: 'I do not love you'
      });
 
    console.log("after promise!");
};

askYourGirlFriend();
Babel Compiler v6.4.4 Copyright (c) 2014-2015 Sebastian McKenzie
?