1

I have code like this:

const loop1 = length => new Promise((resolve, reject) => {
  try {
    let b = 0
    for (let i = 0; i < length; i++) b = b + i
    resolve(b)
  } catch (e) {
    reject(e)
  }
})

const loop2 = length => new Promise((resolve, reject) => {
  try {
    let b = 0
    for (let i = 0; i < length; i++) b = b + i
    resolve(b)
  } catch (e) {
    reject(e)
  }
})

const startTime = new Date().getTime()

loop1(10000000000).then(result => {
  const endTime = new Date().getTime()
  const duration = endTime - startTime
  console.log(`loop1: ${duration}`, result)
}).catch(error => console.log('loop1 error:', error))

loop2(1).then(result => {
  const endTime = new Date().getTime()
  const duration = endTime - startTime
  console.log(`loop2: ${duration}`, result)
}).catch(error => console.log('loop2 error:', error))

const endTime = new Date().getTime()
const duration = endTime - startTime
console.log('duration', duration)

Why are the results like this?:

root@ububtu:~$ node .
duration 15539
loop1: 15545 49999999990067860000
loop2: 15545 0

Why isn't the result like this?:

root@ububtu:~$ node .
duration 0
loop2: 5 0
loop1: 15545 49999999990067860000

Why should wait loop1 for give the result? Why not passed loop1 to give result loop2 first? And why the duration time is not < 1 seconds but more than 15 seconds?

0

1 Answer 1

2

The function you pass into the Promise constructor (the promise executor function) is called synchronously. That's so that it can start the asynchronous process (whatever it is) that the promise represents.

So in your code, loop1's executor function runs synchronously, then the promise is returned, then loop2's executor function is run synchronously; later, the fulfillment handlers are called asynchronously.

Remember: Promises don't turn a synchronous thing into an asynchronous thing. They just provide a standardized means of observing the results of things that are already asynchronous.

If you update your code to model asynchronous operations (in this case I'll use setTimeout), you'll see loop2's handler called before loop1's, since it fulfills earlier:

const loop1 = length => new Promise((resolve) => {
  setTimeout(resolve, length, length)
})

const loop2 = length => new Promise((resolve) => {
  setTimeout(resolve, length, length)
})

const startTime = new Date().getTime()

loop1(800).then(result => {
  const endTime = new Date().getTime()
  const duration = endTime - startTime
  console.log(`loop1: ${duration}`, result)
}).catch(error => console.log('loop1 error:', error))

loop2(1).then(result => {
  const endTime = new Date().getTime()
  const duration = endTime - startTime
  console.log(`loop2: ${duration}`, result)
}).catch(error => console.log('loop2 error:', error))

const endTime = new Date().getTime()
const duration = endTime - startTime
console.log('duration', duration)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.