Published
Edited
Nov 21, 2021
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const func_multiply = new Function("arg1", "arg2", "return arg1 * arg2;");
return func_multiply(5, 10);
}
Insert cell
Insert cell
function greet(name, callback) {
console.log('Hi' + ' ' + name);
callback();
}
Insert cell
// callback function
function callMe() {
console.log('Callback!');
}
Insert cell
Insert cell
Insert cell
{setTimeout(() => {
setTimeout(() => {
setTimeout(() => {
setTimeout(() => {
//и т.д.
}, 5000)
}, 5000)
}, 5000)
}, 5000)}
Insert cell
Insert cell
Insert cell
{
let promise = new Promise(function(resolve, reject) {
// через 1 секунду сигнализировать, что задача выполнена с результатом "done"
setTimeout(() => resolve("done"), 1000);
})
let promise_reject = new Promise(function(resolve, reject) {
// спустя одну секунду будет сообщено, что задача выполнена с ошибкой
setTimeout(() => reject(new Error("Whoops!")), 1000);
});
}
Insert cell
Insert cell
{
let promise = Promise.resolve();
promise.then(() => alert("Промис выполнен"));
alert("Код выполнен");
}
Insert cell
Insert cell
{
Promise.resolve()
.then(() => "Промис выполнен")
.then(() => "Код выполнен");
}
Insert cell
Insert cell
{
new Promise(function(resolve, reject) {
setTimeout(() => resolve(1), 1000); // (*)
}).then(function(result) { // (**)
alert(result); // 1
return result * 2;

}).then(function(result) { // (***)
alert(result); // 2
return result * 2;
}).then(function(result) {
alert(result); // 4
return result * 2;
});
}
Insert cell
Insert cell
Insert cell
Insert cell
{
Promise.all([
new Promise(resolve => setTimeout(() => resolve(1), 3000)), // 1
new Promise(resolve => setTimeout(() => resolve(2), 2000)), // 2
new Promise(resolve => setTimeout(() => resolve(3), 1000)) // 3
]).then(alert); // когда все промисы выполнятся, результат будет 1,2,3
// каждый промис даёт элемент массива
}
Insert cell
Insert cell
{
const promise1 = Promise.resolve(3);
const promise2 = new Promise((resolve, reject) => setTimeout(reject, 100, 'foo'));
const promises = [promise1, promise2];
Promise.allSettled(promises).
then((results) => results.forEach((result) => console.log(result.status)));
// expected output:
// "fulfilled"
// "rejected"
}
Insert cell
Insert cell
Promise.race([
new Promise((resolve, reject) => setTimeout(() => resolve(3), 1000)),
new Promise((resolve, reject) => setTimeout(() => reject(new Error("Ошибка!")), 2000)),
new Promise((resolve, reject) => setTimeout(() => resolve(1), 1000))
]).then(alert); // 1. Быстрее всех выполнится последний промис.
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
async function f() {
return 1;
}
f().then(alert); // 1
}
Insert cell
Insert cell
{
async function f() {
let promise = new Promise((resolve, reject) => {
setTimeout(() => resolve("готово!"), 1000)
});
let result = await promise; // будет ждать, пока промис не выполнится (*)
alert(result); // "готово!"
}
return f();
}
Insert cell
Insert cell
Insert cell
Insert cell
function vanillaJS() {
var count = 0;
var button = document.querySelector('button');
button.addEventListener('click', () => console.log(`Clicked ${++count} times`));
}
Insert cell
function RxJS() {
var button = document.querySelector('button');
rxjs.fromEvent(button, 'click').pipe(
rxjs.operators.scan(count => count + 1, 0)
).subscribe(count => console.log(`Clicked ${count} times`));
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more