1
00:00:00,750 --> 00:00:09,990
In the last lecture we looked at callbacks as a way to write asynchronous code callback functions are

2
00:00:10,140 --> 00:00:18,720
really really easy to implement and they're pretty simple callback functions How ever can become quite

3
00:00:18,810 --> 00:00:29,280
messy and complicated to maintain as we nest functions within functions especially has the level of

4
00:00:29,280 --> 00:00:32,460
nesting increases.

5
00:00:32,640 --> 00:00:44,730
So there is another way to write asynchronous code and this is call as the concept of promises callback

6
00:00:44,730 --> 00:00:49,970
function runs when it is triggered by the calling function.

7
00:00:50,010 --> 00:00:56,940
So it in effect will wait until it is called by the calling function.

8
00:00:57,050 --> 00:01:03,590
Similarly a promise is a way to wait for something to happen.

9
00:01:03,600 --> 00:01:09,970
Promises provide a much cleaner way to write asynchronous code.

10
00:01:10,890 --> 00:01:18,080
Let's understand this with an example.

11
00:01:18,400 --> 00:01:22,000
Let's say we write function do stuff

12
00:01:27,120 --> 00:01:29,580
that takes into arguments data

13
00:01:33,950 --> 00:01:42,470
and a callback and it simply calls the callback function with a string say the

14
00:02:18,330 --> 00:02:23,420
we can write this exact same function using promises.

15
00:02:23,470 --> 00:02:31,020
So let's cut and out this code and see how to rewrite it using promises.

16
00:02:31,080 --> 00:02:36,250
So we do it like a function do stuff.

17
00:02:36,510 --> 00:02:39,840
And this time there is a single argument.

18
00:02:39,850 --> 00:02:45,490
Data will need a callback function when using promises.

19
00:02:45,570 --> 00:02:58,350
What we are going to do is return a promise a promise is something you can wait on for it to either

20
00:02:58,740 --> 00:03:05,260
resolve or reject or in other words either succeed or fail.

21
00:03:05,430 --> 00:03:14,280
So if there is an error during the execution of the function it could reject the promise and if it completes

22
00:03:14,340 --> 00:03:17,790
successfully you could resolve the wrongs

23
00:03:21,150 --> 00:03:21,960
first.

24
00:03:21,960 --> 00:03:25,490
We're going to use the return keyword.

25
00:03:25,890 --> 00:03:28,600
We have to return the promise.

26
00:03:28,620 --> 00:03:31,450
So the caller has access to it.

27
00:03:31,860 --> 00:03:34,740
So we returned a new promise.

28
00:03:34,740 --> 00:03:42,340
Promise is a predefined construct that takes in our function as an argument

29
00:03:46,260 --> 00:03:56,490
and this function in turn takes two arguments resolve and reject what what we are doing here is creating

30
00:03:56,550 --> 00:04:01,030
a new promise and returning it to the caller.

31
00:04:01,140 --> 00:04:04,880
So the caller can use it as needed.

32
00:04:06,930 --> 00:04:18,080
So if everything goes well you can call resolve and if there's an error we could call reject.

33
00:04:18,100 --> 00:04:20,800
So let's say a type of data

34
00:04:27,010 --> 00:04:29,740
travel equal to encodes Boullion

35
00:04:35,810 --> 00:04:38,360
and data table equal to

36
00:04:41,240 --> 00:04:41,810
two

37
00:04:46,830 --> 00:04:48,670
we resolved that promise.

38
00:04:56,760 --> 00:05:05,650
Like so otherwise we reject the promise in a similar fashion.

39
00:05:06,020 --> 00:05:16,230
The triple equal to operator are compared to the left hand side with the right hand side and returns

40
00:05:16,230 --> 00:05:27,660
true only if the both sides have the same value as well as the same day that I you can't pass any type

41
00:05:27,660 --> 00:05:32,600
of data as an argument to resolve and reject.

42
00:05:32,670 --> 00:05:41,910
So you could best ratings numbers booleans or object only thing to remember here is you can't pass only

43
00:05:42,000 --> 00:05:43,480
one argument.

44
00:05:43,620 --> 00:05:53,700
So the best way to pass this data is by using an object using an object allows you to pass several items

45
00:05:53,700 --> 00:05:57,890
or attributes of the data within a single argument.

46
00:05:58,050 --> 00:06:06,950
So let's pass an object that retains a status and a message string for example.

47
00:06:07,010 --> 00:06:15,750
So if everything goes fine we resolve the promise using an object that saves status

48
00:06:20,590 --> 00:06:23,950
it's success and message is

49
00:06:26,810 --> 00:06:28,150
all is well.

50
00:06:28,400 --> 00:06:32,660
And if there is an error we reject a promise with an object

51
00:06:42,910 --> 00:06:43,820
with status

52
00:06:48,500 --> 00:06:50,680
as editor and message as

53
00:06:54,990 --> 00:06:55,510
loops.

54
00:06:55,520 --> 00:06:56,420
There was

55
00:07:00,750 --> 00:07:05,150
an editor something like that

56
00:07:28,870 --> 00:07:35,100
and that's come into our the reject far for now and run this function

57
00:07:40,530 --> 00:07:45,570
we call the function that uses promises just like any other function.

58
00:07:45,570 --> 00:07:51,480
So our function do stuff takes one argument core data.

59
00:07:51,810 --> 00:07:55,380
So I'm going to pass in a boolean value.

60
00:07:55,420 --> 00:08:05,790
That's true so that if block of our function runs so we call the function and then we call then function

61
00:08:05,790 --> 00:08:08,320
with a dot operator like.

62
00:08:08,320 --> 00:08:20,270
So dot then which is a function that's just part of the chain and then takes our two callbacks.

63
00:08:20,400 --> 00:08:24,420
The first one is fine if everything goes well.

64
00:08:24,750 --> 00:08:34,150
So we call dark then which is a function and pass it our Frist function.

65
00:08:34,170 --> 00:08:48,810
If everything goes well that simply brings the message in the output.

66
00:08:49,040 --> 00:08:53,780
Now let's go ahead and run this to see how it goes.

67
00:08:54,050 --> 00:09:06,590
We should see the Sukses message printed in the output Arsa we can do stuff that doesn't actually do

68
00:09:06,680 --> 00:09:08,540
anything.

69
00:09:08,690 --> 00:09:15,990
It simply calls resolve if we pass in a boolean to as input.

70
00:09:16,400 --> 00:09:20,190
So we see success message on the screen.

71
00:09:21,290 --> 00:09:26,590
We can do the exact same thing with the errors.

72
00:09:26,750 --> 00:09:37,490
So let's Ancrum in the reject part of the function the catch this error we must add a second argument

73
00:09:37,490 --> 00:09:43,390
to our then function and this will receive our error.

74
00:09:44,060 --> 00:09:47,060
And now we have two functions.

75
00:09:47,070 --> 00:09:53,700
One when everything goes well and one then something goes wrong.

76
00:09:53,780 --> 00:10:05,300
So if something breaks said call our function now to simulate a response real fast false or anything

77
00:10:05,330 --> 00:10:15,730
other than Boolean to to our DO step function and you can see that the error message brings to the screen.

78
00:10:16,230 --> 00:10:25,400
Another advantage of using promises is there is all and reject functions can only be called at most

79
00:10:25,490 --> 00:10:27,600
once.

80
00:10:28,550 --> 00:10:35,560
And contrast read callback functions if you accidentally invoke a callback function twice.

81
00:10:35,570 --> 00:10:38,270
It will be executed two times.

82
00:10:38,800 --> 00:10:48,470
However with promises even if you call the resolve or the rejet several times only the first call to

83
00:10:48,470 --> 00:10:58,370
resolve or reject ever gets executed any subsequent calls to resolve or reject are always ignored in

84
00:10:58,370 --> 00:10:59,320
the next lecture.

85
00:10:59,380 --> 00:11:06,380
See how the chain these promises together and call them one after another.

86
00:11:06,860 --> 00:11:08,600
So let's keep going.
