Câu hỏi

Kết quả đoạn code sau là gì?

  const myPromise = () => Promise.resolve("I have resolved!");
  function firstFunction() {
  myPromise().then((res) => console.log(res));
  console.log("second");
  }
  async function secondFunction() {
  console.log(await myPromise());
  console.log("second");
  }
  firstFunction();
  secondFunction();
  • A: I have resolved!, secondI have resolved!, second
  • B: second, I have resolved!second, I have resolved!
  • C: I have resolved!, secondsecond, I have resolved!
  • D: second, I have resolved!I have resolved!, second

Câu trả lời

Đáp án: D

Có thể tưởng tượng đơn giản cách promise thực thi như sau: _bây giờ tôi sẽ để tạm nó sang một bên vì nó tính toán mất thời gian. Chỉ khi nào nó được hoàn thành (resolved) hay bị hủy bỏ (rejecte...

Bạn cần đăng nhập để xem