1
00:00:00,150 --> 00:00:06,150
All right, so this works, we have our staff function, it is a Synchronoss we're waiting for problems

2
00:00:06,150 --> 00:00:09,930
to resolve and we can see that it is already much cleaner.

3
00:00:10,410 --> 00:00:14,880
How we can set up this code without the wrapping function?

4
00:00:15,450 --> 00:00:22,320
Well, what's really called that in node, there is a module by the name of your Utah and we just go

5
00:00:22,320 --> 00:00:28,980
here, you consed, and we'll sign it to a variable and we'll just be looking for require, of course,

6
00:00:29,310 --> 00:00:31,380
since we would want to import the module.

7
00:00:31,630 --> 00:00:33,930
And again, the name is a utility.

8
00:00:34,680 --> 00:00:41,700
And instead of this module, we have a method by the name of promise, by and with the use of this function

9
00:00:41,940 --> 00:00:48,660
we can take our read file, which of course was looking for a callback and turn it into the function

10
00:00:48,660 --> 00:00:50,550
that returns a promise.

11
00:00:50,820 --> 00:00:54,570
And I got to say is over simplified version, what is happening there.

12
00:00:54,570 --> 00:01:00,180
But hopefully you understand that as a result will get again this promise back.

13
00:01:00,570 --> 00:01:05,820
Now, the way we'll set that up, of course, we have read file here already, so I cannot use the same

14
00:01:05,820 --> 00:01:06,150
name.

15
00:01:06,370 --> 00:01:13,770
That's why I'll go it read file and we'll call this promise from now on is equal to util.

16
00:01:14,160 --> 00:01:15,780
That is of course my module dot.

17
00:01:15,780 --> 00:01:21,910
And then notice the function promise if I and then we pass in that read file function.

18
00:01:22,080 --> 00:01:23,540
So now I have my promise one.

19
00:01:23,850 --> 00:01:28,260
So now what I could do is first of all come into S and moving down.

20
00:01:29,170 --> 00:01:38,500
Again, so it stays for your reference, but it doesn't compromise my view and of course, now have

21
00:01:38,500 --> 00:01:41,850
the error because, well, get taxed is not defined.

22
00:01:42,250 --> 00:01:42,790
That's fine.

23
00:01:42,790 --> 00:01:45,340
Don't worry and I'll copy and paste.

24
00:01:45,340 --> 00:01:54,520
And since I also want to write the new file, I will get from the file system on the right file function,

25
00:01:54,910 --> 00:02:01,780
and I'll do the same thing where in this case it's going to be called write, file, promise, and then

26
00:02:01,780 --> 00:02:03,760
we pass in right file.

27
00:02:04,240 --> 00:02:12,340
And of course now instead of going get tax, what I'm gonna do is go write file or I'm sorry.

28
00:02:12,550 --> 00:02:15,280
In this case we're going for Red File Promise.

29
00:02:15,610 --> 00:02:19,900
And then keep in mind one thing where this is not a get taxed one.

30
00:02:20,140 --> 00:02:24,370
So we're still looking for the path as well as the encoding.

31
00:02:24,530 --> 00:02:25,840
That's why I'll copy and paste.

32
00:02:26,140 --> 00:02:30,100
And then the second argument we pass in is the encoding.

33
00:02:30,400 --> 00:02:37,420
And again, our functionality shouldn't change where I should see in the console the results from the

34
00:02:37,420 --> 00:02:37,930
first.

35
00:02:39,190 --> 00:02:42,790
Now, of course, I would want to do the same thing with the second one.

36
00:02:43,060 --> 00:02:50,530
So I have read file promised second to the again, we have to pass in the encoding, otherwise we'll

37
00:02:50,530 --> 00:02:51,760
get the buffer back.

38
00:02:52,300 --> 00:02:57,940
So copy this paste and again, if we log it, there is we have.

39
00:02:58,060 --> 00:03:05,110
Hello, this is first text file and this is the second one and it's in the line for we passed in right

40
00:03:05,110 --> 00:03:08,380
file into promised fi course.

41
00:03:08,590 --> 00:03:13,330
What we could also do is right away write the file so I can go away.

42
00:03:13,510 --> 00:03:20,620
And I'm simply not assigning this to a variable because well I'm getting back on the finder as far as

43
00:03:20,620 --> 00:03:29,260
data and I'm just going to go with a wait right file promise and the same spiel we passed in First the

44
00:03:29,260 --> 00:03:29,710
path.

45
00:03:30,010 --> 00:03:36,340
And in this case, of course, if the file is not there, node will create one and the name will be

46
00:03:36,370 --> 00:03:37,300
a result.

47
00:03:37,300 --> 00:03:44,060
And I'll just call this mine Granade and then the next day and then the second one is there.

48
00:03:44,260 --> 00:03:50,260
So for the last one, I'll set up my template string and I'll just say, this is awesome.

49
00:03:50,470 --> 00:03:51,100
Awesome.

50
00:03:51,400 --> 00:03:58,630
And then let's set up the colon and then we'll access both first as well as the second one.

51
00:03:58,640 --> 00:04:01,180
So interpolate most of them.

52
00:04:02,110 --> 00:04:09,550
Second, and then once we save, if we take a look at the content, there it is now we have the mind

53
00:04:09,550 --> 00:04:10,900
grenade one, correct.

54
00:04:11,110 --> 00:04:13,690
Now there's probably one where I was testing.

55
00:04:13,990 --> 00:04:14,410
Yep.

56
00:04:14,410 --> 00:04:16,899
There is one where I was testing Salamone this one.

57
00:04:17,200 --> 00:04:22,280
But if I take a look at it one and of course I'll have this is awesome.

58
00:04:22,300 --> 00:04:22,780
Hello.

59
00:04:23,020 --> 00:04:26,670
This is first text file and then I have the second one.

60
00:04:26,980 --> 00:04:32,540
Now what's even more cool that technically we can even skip this part as well?

61
00:04:32,560 --> 00:04:35,470
Again, I'll leave this one for your reference.

62
00:04:36,010 --> 00:04:44,710
But if you go to require, um, and you require, of course, filesystem module, but if you add dot

63
00:04:44,710 --> 00:04:47,530
promises, what do you think is going to return?

64
00:04:47,950 --> 00:04:49,550
Well, effectively the same thing.

65
00:04:49,870 --> 00:04:56,860
So now of course I just need to change the name from read file promise and write a promise back to read

66
00:04:56,860 --> 00:05:03,550
file and write file, since those are the two functions that I'm importing and they will write away

67
00:05:03,550 --> 00:05:04,920
way return promises.

68
00:05:04,930 --> 00:05:10,160
And again, we save and remember that by default we're just overwriting stuff.

69
00:05:10,480 --> 00:05:19,600
So if I were to go and add a third argument, my options one and if I'm just going to say flag and I'll

70
00:05:19,600 --> 00:05:24,070
set it equal to upend once we save one, now we have.

71
00:05:24,340 --> 00:05:25,260
This is awesome.

72
00:05:25,420 --> 00:05:31,120
Repeat it one more time again just to showcase that our functionality still works.

73
00:05:31,750 --> 00:05:40,240
I fully understand that there was a little detour where we created our own wrapper function and we covered

74
00:05:40,480 --> 00:05:43,030
how we would consume promises.

75
00:05:43,360 --> 00:05:48,910
But hopefully this helps you understand that this approach is definitely way better.

76
00:05:49,390 --> 00:05:51,040
So it is more readable.

77
00:05:51,340 --> 00:05:54,880
It is more easier to wrap your head around.

78
00:05:55,180 --> 00:06:00,130
And that's why for the remainder of the course we'll stick with this one again.

79
00:06:00,370 --> 00:06:04,060
Some things will be provided to us by default.

80
00:06:04,180 --> 00:06:07,180
So libraries will provide the functions that return.

81
00:06:07,180 --> 00:06:13,090
Promises will right away have the callback functions that we'll just set up as async and hopefully you

82
00:06:13,090 --> 00:06:13,720
get the gist.

83
00:06:14,020 --> 00:06:20,680
So a lot of things will be given to us, but the core functionality won't change where instead of using

84
00:06:20,680 --> 00:06:27,700
callbacks and testing them instead of the other callbacks and nothing more and more and more, we will

85
00:06:27,700 --> 00:06:36,520
set up everything with async away because the syntax error is cleaner and we're easier to read.

