An Introduction to Node.js
By me, Kyle Mitofsky, a developer
... an asynchronous, event-driven, JavaScript runtime, designed to build scalable network applications
Language
&
Runtime
- ECMAScript 20??
- Callbacks
- Promises
- Async / Await
// function declaration
function SayHi (name) {
console.log(`Hi: ${name}`)
}
// function expression / assignment
let SayHey = function (name) {
console.log(`Hey: ${name}`)
}
// arrow function / lambda
SayHello (name) => {
console.log(`Hello: ${name}`)
}
Note: func delcarations -> are hoisted to the top of the scope (immediately available).
arrow func -> lecical this (defined by outer scope)
SayHi('kyle');
SayHi.Call(this, 'kyle'); // C for Comma
SayHi.Apply(this, ['kyle']); // A for Array
function Multiply(a, b) {
return a * b
}
// invoke
let result = Multiply(2,5)
console.log(result)
function Multiply(a, b, callback) {
callback(a * b) // pretend this took a while
}
// invoke w/ anonymous function
Multiply(2,5, (result) => {
console.log(result)
})
// wrap a callback in a promise
function MultiplyPromise(a,b) {
return new Promise((resolve) => {
Multiply(a, b, (result) => {
// resolve promise from in callback
resolve(result)
});
});
}
let multPromise = MultiplyPromise(a,b)
multPromise.then(result => {
console.log(result)
})
(async function(){
let result = await MultiplyPromise(a,b)
console.log(result)
})()
.NET | Node | |
---|---|---|
Language | C# / VB / F# | JS / TS |
Runtime | MSIL | V8 |
Platform | Win, Mac, Linux | Win, Mac, Linux |
Packages | Nuget | npm |