// Исполняется после вызова лупа
let loopAsync = ms => new Promise(resolve => setTimeout(resolve, ms * 1000));
const getSomeTextAsync = async (text, time) =>
`value - ${text} was showed with delay ${time} and while is ${await loopAsync(time)}`;
const showTextAsync = async (sometext, time) => {
// блокирующий await
sometext = await getSomeTextAsync(sometext, time);
console.log(sometext);
}
// Основной поток
showTextAsync('something that works in async way', 4); // 1 вызов функции с блокирующим авейтом внутри
showTextAsync('something that works in async way', 1); // 2 вызов функции с блокирующим авейтом внутри
console.log('something that work in main thread');
// console show
// something that work in main thread
// something that works in async way was showed with delay 1
// something that works in async way was showed with delay 4
class Person {
constructor(props) {
Object.keys(props).forEach(key => this[key] = props[key]);
}
static createPerson(name, surname, age) {
return new Person({ name: name, surname: surname, age: age });
}
changeAge(num) {
this.age = this.age + num;
return this;
}
}
const NEWPERSON = Person.createPerson('Bill', 'Gates', 22);
console.log(NEWPERSON); // { name: 'Bill', surname: 'Gates', age: 22 }
console.log(NEWPERSON.changeAge(5)); // { name: 'Bill'', surname: 'Gates', age: 27 }
class myClass {
constructor(props) {
if (typeof props == 'object') {
Object.keys(props).forEach(key => this[key] = props[key]);
}
}
static createSomeObj() {
return new myClass({ 1: 1, 2: 2 });
}
}
const instance = new myClass();