Modern Full-Stack Development



Download 5,64 Mb.
Pdf ko'rish
bet40/107
Sana06.08.2021
Hajmi5,64 Mb.
#140576
1   ...   36   37   38   39   40   41   42   43   ...   107
Bog'liq
Modern Full-Stack Development Using TypeScript, React, Node

 Utilities  (util)
The final module we’re going to look at briefly is a little bit of a catch-all, or maybe it’s 
better to say it contains functions that support Node internally, but which also provides 
generic JavaScript functionality that might be useful to application code.
For example, if you want to format a string using a printf-like format, you can do
util.format("%s:%s", "aa", "bb", "cc") // "aa:bb cc"
What if you have an asynchronous function, like fs.stat() let’s say, that uses the 
common pattern seen in Node where the callback receives an error object first and 
then a value object and you want to make it use Promises, but you don’t want to have to 
rewrite the code? Well, then there’s this:
const newStat = util.promisify(fs.stat);
Now, you can call it like so:
newStat("/home/fzammetti")
.then((inStats) => { console.log(inStats);
})
.catch((inError) => { console.log(inError);
});
Nice, right?
Do you have a variable and need to know its type? Then Utilities has you covered: 
util.types.isArrayBuffer(), util.types.isBigInt64Array(), util.types.
isBooleanObject(), util.types.isDate(), util.types.isMap(), util.types.
isPromise(), and util.types.isUint32Array() are available just to name a few.
Finally, the util.inspect() method can be used to get a string representation of an 
object intended for debugging purposes. This method accepts a whole bunch of options, 
including being able to specify whether you want to show hidden fields, how far down 
into the object to go if it contains nested objects (depth) and if you wish to sort the keys 
in the stringified object that is returned. It’s important to note though that this method’s 
documentation states that the representation this produces can change at any time, so 
you, therefore, should never try to use this programmatically. Printing the string to the 
console is fine, but trying to parse it wouldn’t be a great idea lest you find your code 
randomly broke at some point in the future after a Node upgrade.
Chapter 2   a Few More words: advanCed node and npM


40

Download 5,64 Mb.

Do'stlaringiz bilan baham:
1   ...   36   37   38   39   40   41   42   43   ...   107




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish