Giter VIP home page Giter VIP logo

promise_interview_questions's Introduction

Questions

Detailed explanation is in ANSWERS-ENG.md.
Пошаговое решение в ANSWERS-RU.md.

  • Question 1
console.log('start');

const promise1 = new Promise((resolve, reject) => {
  console.log(1)
})

console.log('end');
Answer start, 1, end
  • Question 2
console.log('start');

const promise1 = new Promise((resolve, reject) => {
  console.log(1)
  resolve(2)
})

promise1.then(res => {
  console.log(res)
})

console.log('end');
Answer start , 1 , end и 2
  • Question 3
console.log('start');

const promise1 = new Promise((resolve, reject) => {
  console.log(1)
  resolve(2)
  console.log(3)
})

promise1.then(res => {
  console.log(res)
})

console.log('end');
Answer start , 1 , 3 , end и 2
  • Question 4
console.log('start');

const promise1 = new Promise((resolve, reject) => {
  console.log(1)
})

promise1.then(res => {
  console.log(2)
})

console.log('end');
Answer start , 1 , end
  • Question 5
console.log('start')

const fn = () => (new Promise((resolve, reject) => {
  console.log(1);
  resolve('success')
}))

console.log('middle')

fn().then(res => {
  console.log(res)
})

console.log('end')
Answer start , middle, 1 , end и success
  • Question 6
console.log('start')

Promise.resolve(1).then((res) => {
  console.log(res)
})

Promise.resolve(2).then((res) => {
  console.log(res)
})

console.log('end')
Answer start , end , 1 и 2
  • Question 7
console.log('start')

setTimeout(() => {
  console.log('setTimeout')
})

Promise.resolve().then(() => {
  console.log('resolve')
})

console.log('end')
Answer start , end , resolve и setTimeout
  • Question 8
const promise = new Promise((resolve, reject) => {
  console.log(1);
  setTimeout(() => {
    console.log("timerStart");
    resolve("success");
    console.log("timerEnd");
  }, 0);
  console.log(2);
});

promise.then((res) => {
  console.log(res);
});

console.log(4);
Answer 1, 2, 4, timeStart, timeEnd, success
  • Question 9
const timer1 = setTimeout(() => {
  console.log('timer1');
  
  const promise1 = Promise.resolve().then(() => {
    console.log('promise1')
  })
}, 0)

const timer2 = setTimeout(() => {
  console.log('timer2')
}, 0)
Answer timer1, promise1, timer2
  • Question 10
console.log('start');

const promise1 = Promise.resolve().then(() => {
  console.log('promise1');
  const timer2 = setTimeout(() => {
    console.log('timer2')
  }, 0)
});

const timer1 = setTimeout(() => {
  console.log('timer1')
  const promise2 = Promise.resolve().then(() => {
    console.log('promise2')
  })
}, 0)

console.log('end');
Answer start, end, promise1, timer1, promise2, timer2
  • Question 11
async function asyncFunc() {
  console.log(2);
  await Promise.resolve();
  console.log(4);
}
console.log(1);
asyncFunc();
console.log(3);
Answer 1, 2, 3, 4
  • Question 12
Promise.resolve(1)
  .then(2)
  .then(Promise.resolve(3))
  .then(console.log);
Answer 1
  • Question 13
console.log('first');
setTimeout(() => console.log('second'), 0);
new Promise((resolve, reject) => {
  console.log('third');
  resolve();
}).then(() => console.log('fourth'));
console.log('fifth');
Answer first, third, fifth, fourth, second
  • Question 14
setTimeout(() => console.log("1"), 0);
new Promise((resolve, reject) => {
  console.log("2");
  for (let i = 0; i < 10000; i++) {
    if (i === 9999) resolve();
  }
  console.log("3");
}).then(() => console.log("4"));
console.log("5");
Answer 2, 3, 5, 4, 1
  • Question 15
Promise.reject('error')
  .then(res => console.log('success:', res), err => console.log('fail:', err))
Answer fail: error
  • Question 16
async function func() {
  console.log(1);
  await new Promise(resolve => setTimeout(resolve, 1000));
  console.log(3)
}
console.log(2);
func();
console.log(4);
Answer 2, 1, 4, 3
  • Question 17
Promise.resolve()
  .then(() => {
    console.log('then1');
    Promise.resolve().then(() => console.log('then1.1'));
  })
  .then(() => console.log('then2'));
Answer then1, then1.1, then2
  • Question 18
setTimeout(() => console.log('a'), 0);
Promise.resolve().then(() => console.log('b')).then(() => console.log('c'));
Answer b, c, a
  • Question 19
console.log('start');
setTimeout(() => console.log('setTimeout'), 0);
Promise.resolve().then(() => console.log('promise'));
console.log('end');
Answer start, end, promise, setTimeout
  • Question 20
async function test() {
  console.log(1);
  await async function() { console.log(2) }();
  console.log(3);
}
console.log(4);
test();
console.log(5);
Answer 4, 1, 2, 5, 3

promise_interview_questions's People

Contributors

melkoto avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.