Co-founder & Backend @ Coding Sans
function timeout(duration = 0) {
return new Promise((resolve, reject) => {
setTimeout(resolve, duration);
})
}
const p = timeout(1000).then(() => {
return timeout(2000);
}).then(() => {
throw new Error('hmm');
}).catch(err => {
return Promise.all([timeout(100), timeout(200)]);
})
const bob = {
_name: 'Bob',
_friends: [],
printFriends() {
this._friends.forEach(f =>
console.log(this._name + ' knows ' + f));
}
};
function f() {
let x;
{
// okay, block scoped name
const x = 'sneaky';
// error, const
x = 'foo';
}
// okay, declared with `let`
x = 'bar';
// error, already declared in block
let x = 'inner';
}
function* idMaker(){
let index = 0;
while (index < 3) {
yield index++;
}
}
const gen = idMaker();
console.log(gen.next().value); // 0
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
console.log(gen.next().value); // undefined
class Cat {
constructor(name) {
this.name = name;
}
speak() {
console.log(this.name + ' makes a noise.');
}
}
class Lion extends Cat {
speak() {
super.speak();
console.log(this.name + ' roars.');
}
}
const name = 'Bob', time = 'today';
const str1 = `Hello ${name}, how are you ${time}?`
const str2 = `I am
multiline!`
asyncFn1(function(err, obj1) {
if (err) {
//error handling
return;
}
asyncFn2(function(err, obj2) {
if (err) {
//error handling
return;
}
console.log('success');
});
});
asyncFn1()
.then(function() {
return asyncFn2();
})
.then(function() {
console.log('success');
})
.catch(function(err) {
//error handling
});
co(function * asyncGeneratorFn() {
try {
const obj1 = yield asyncFn1();
const obj2 = yield asyncFn2();
} catch(err) {
//error handling
}
console.log('success');
});
async function asyncFn() {
try {
const obj1 = await asyncFn1();
const obj2 = await asyncFn2();
} catch(err) {
//error handling
}
console.log('success');
};
Thanks for your attention!