You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+34Lines changed: 34 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -407,3 +407,37 @@ Use ``nextTick()`` when you want to make sure that in next event loop iteration
407
407
 Did you notice how **async functions** are different compared to a **promise.then**? The ``await`` keyword suspends the ``async function``, whereas the ``Promise`` body would've kept on being executed if we would've used ``.then``!
408
408
409
409
 Read more in the [section with code examples](https://github.com/SKindij/Asynchronous-Programming-Node.js/tree/main/codeSamples)...
410
+
411
+
- - -
412
+
413
+
 ``util.promisify()`` is utility that allows you to convert function that uses Node.js-style callback pattern into function that returns promise.\
414
+
> _Node.js-style callbacks usually follow pattern of having error object as first argument and result as second argument, like this:_
415
+
> > ```javascript
416
+
>>functionreadFile(path, callback) {
417
+
>>fs.readFile(path, (err, data) => {
418
+
>>if (err) {
419
+
>>callback(err, null);
420
+
>> } else {
421
+
>>callback(null, data);
422
+
>> }
423
+
>> });
424
+
>> }
425
+
>>```
426
+
> _With util.promisify(), you can convert this function into promise-based function, like this:_
427
+
> > ```javascript
428
+
>>constutil=require('util');
429
+
>>constfs=require('fs');
430
+
>>// to convert fs.readFile() into promise-based function readFile
431
+
>>constreadFile=util.promisify(fs.readFile);
432
+
>>
433
+
>>asyncfunctionexample() {
434
+
>>try {
435
+
>>// to wait for data to be read from file before logging it to console
0 commit comments