Untitled

 avatar
user_7147158
plain_text
4 months ago
6.9 kB
4
Indexable
// let a = 10;
// (function() {
//     console.log(a);
//     var a = 20;
//     console.log(a);
// })();
// let obj = { a: 1, b: 2 };
// let newObj = { ...obj, a: 3 };
// console.log(newObj);

// const a = {};
// const b = { key: "b" };
// const c = { key: "c" };
// a[b] = 123;
// a[c] = 456;
// console.log(a[b]);

// let x = 5;
// let y = x++;
// console.log(x, y);

// console.log(typeof NaN);

// let arr = [1, 2, 3];
// arr[10] = 42;
// console.log(arr.length);
// console.log(arr);

// function outerFunc() {
//     var x = 10;
//     return function innerFunc() {
//         console.log(x);
//         var x = 20;
//     };
// }
// const fn = outerFunc();
// fn();

// const obj = {
//     value: 42,
//     method: function () {
//         console.log(this.value);
//     }
// };
// obj.method();

// console.log("Start");
// setTimeout(() => console.log("Timeout"), 0);
// Promise.resolve().then(() => console.log("Promise"));
// console.log("End");

// console.log(Object.is(22, 22));
// console.log(+0 === -0);
// console.log(Object.is(NaN, NaN));
// console.log(NaN === NaN);

// console.log("Start");
// setTimeout(() => console.log("Timeout"), 0);
// Promise.resolve().then(() => console.log("Promise 1")).then(() => console.log("Promise 2"));
// console.log("End");

// console.log("Start");
// Promise.resolve().then(() => {
//     console.log("Promise 1");
//     return Promise.resolve("Promise 2");
// }).then((value) => {
//     console.log(value);
// });
// console.log("End");

// console.log("A");
// setTimeout(() => console.log("B"), 100);
// Promise.resolve().then(() => {
//     console.log("C");
//     setTimeout(() => console.log("D"), 50);
// });
// console.log("E");

// console.log("A");
// setTimeout(() => console.log("B"), 0);
// Promise.resolve().then(() => {
//     console.log("C");
//     setTimeout(() => console.log("D"), 0);
// });
// console.log("E");
// a e c b d

// console.log("Start");
// setTimeout(() => console.log("Timeout"), 0);
// Promise.resolve().then(() => {
//     console.log("Promise");
//     while (Date.now() % 10 !== 0) {}
//     console.log("Blocking loop finished");
// });
// console.log("End");

// console.log("Start");
// Promise.resolve("Resolved").then((value) => {
//     console.log(value);
// });
// console.log("End");

// console.log("Start");
// Promise.resolve().then(() => {
//     console.log("Step 1");
//     return Promise.resolve("Step 2");
// }).then((value) => {
//     console.log(value);
//     return "Step 3";
// }).then(console.log);
// console.log("End");
// start end step1 step2 step3

// recheck this imp
// async function async1() {
//     console.log("Async1Start");
//     await async2();
//     console.log("Async1End");
// }
// async function async2() {
//     console.log("Async2");
// }
// console.log("Start");
// async1();
// Promise.resolve().then(() => console.log("Promise"));
// console.log("End");
// start end  async1start async2  async1end promise

// recheck this
// setTimeout(() => console.log("Timeout 1"), 0);
// Promise.resolve().then(() => {
//     console.log("Promise 1");
//     setTimeout(() => console.log("Timeout 2"), 0);
// });
// setTimeout(() => console.log("Timeout 3"), 0);
// Promise.resolve().then(() => console.log("Promise 2"));
// promise1 promise2 timeout1 timeout3 timeout2

// setTimeout(() => {
//     console.log("1");
//     Promise.resolve().then(() => console.log("2"));
//     console.log("3");
// }, 0);
// Promise.resolve().then(() => console.log("4"));
// 4 1 3 2

// recheck this
// async function foo() {
//     console.log("1");
//     await bar();
//     console.log("2");
// }
// async function bar() {
//     console.log("3");
// }
// console.log("4");
// foo();
// console.log("5");
// 4 1 3 5 2

// console.log("1");
// Promise.resolve().then(() => {
//     console.log("2");
//     return new Promise((resolve) => {
//         setTimeout(() => {
//             console.log("3");
//             resolve();
//         }, 1000);
//     });
// }).then(() => {
//     console.log("4");
// });
// console.log("5");
// 1 5 2 3 4




async function async1() {
  console.log("1");
  await async2();
  console.log("2");
}
async function async2() {
  console.log("3");
  return Promise.resolve().then(() => console.log("4"));
}
console.log("5");
async1();
Promise.resolve()
  .then(() => console.log("6"))
  .then(() => console.log("7"));
console.log("8");
// 5 1 3 4 8




Promise.resolve()
  .then(() => {
    console.log("Step 1");
    return Promise.resolve("Step 2");
  })
  .then((value) => {
    console.log(value);
    return new Promise((resolve) => {
      setTimeout(() => resolve("Step 3"), 1000);
    });
  })
  .then(console.log)
  .finally(() => console.log("Finally"));



// 4
async function test() {
  console.log("Test Start");
  await Promise.resolve();
  console.log("Test After First Await");
  await Promise.resolve();
  console.log("Test After Second Await");
}
console.log("Script Start");
test();
Promise.resolve().then(() => console.log("Promise Outside"));
console.log("Script End");



//5
async function foo() {
  console.log("Foo Start");
  return "Foo End";
}
async function bar() {
  console.log("Bar Start");
  return foo();
}
console.log("Script Start");
bar().then(console.log);
console.log("Script End");



// 6
async function test() {
  console.log("Before Await");
  await 42;
  console.log("After Await");
}
console.log("Script Start");
test();
console.log("Script End");




// 7
setTimeout(() => {
  console.log("Timeout Start");
  Promise.resolve()
    .then(() => console.log("Promise Inside Timeout"))
    .then(() => setTimeout(() => console.log("Recursive Timeout"), 0));
  console.log("Timeout End");
}, 0);
Promise.resolve().then(() => console.log("Promise Outside Timeout"));



// 8
Promise.race([
  new Promise((resolve) => setTimeout(() => resolve("Timeout 1"), 500)),
  new Promise((resolve) => setTimeout(() => resolve("Timeout 2"), 300)),
  Promise.resolve("Immediate Resolution"),
]).then(console.log);



// 9
Promise.resolve("Step 1")
  .then((value) => {
    console.log(value);
    throw new Error("Error Occurred");
  })
  .catch((err) => {
    console.log(err.message);
    return "Step 2";
  })
  .finally(() => console.log("Finally Block"))
  .then((value) => console.log(value));



//10
async function test() {
  try {
    console.log("Start");
    await Promise.reject("An Error");
    console.log("End");
  } catch (error) {
    console.log("Caught: " + error);
  } finally {
    console.log("Finally");
  }
}
test();
console.log("Script End");
Editor is loading...
Leave a Comment