async function asyncCall(fn, started) {
const result = await fn();
console.log(`Async time: ${Date.now() - started}`);
}
async function profile(fn) {
let started = Date.now();
asyncCall(fn, started);
console.log(`Sync time: ${Date.now() - started}`);
started = Date.now();
}
async function callAsyncMethod(obj, methodName) {
try {
const method = obj[methodName];
try {
await method();
}
catch(err) {
console.error(`replied with ${err}`)
}
} catch(err) {
console.error(err);
process.exit(1);
}
}
class Cart {
async fetchCart(cart) {
const data = await fetch(cart);
this.eventEmitter.emit('sent', data);
}
send(cart) {
this.fetchCart(cart);
return this;
}
render() {
}
}
Я просто обращаю внимание на то, что в некоторых очень узких кейсах использовать .then() будет куда проще и понятнее.
const spoilers=Array.from(document.querySelectorAll(".spoiler"));for(const e of spoilers){const s=e.querySelector(".spoiler-toggle");s.addEventListener("click",()=>{e.classList.contains("is-active")?(e.classList.remove("is-active"),s.innerHTML="Show"):(e.classList.add("is-active"),s.innerHTML="Hide")})}
function async main() {
const p = await send();
console.log(2);
// делаем что-то с p
console.log(3);
}
var query = Posts.findOne({id: id});
var promise = query.exec();
promise.then(function (post) {
var query1 = Comments.find({id: post.id});
var promise1 = query1.exec();
promise1.then(function(comments) {
var query2 = Links.find({id: post.id});
var promise2 = query2.exec();
promise2.then(function(links) {
res.json({post: post, comments: comment, links: links})
})
})
});
Такая конструкция не может быть (насколько мне известно) преобразована в async/await.
скорее, привычка (и незнание конструкции then-catch)