search

Home  >  Q&A  >  body text

javascript - Why does the following promise return this value?

function timeout(ms) {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, ms, 'done');
  });
}

timeout(100).then((value) => {
  console.log(value);
});

Excuse me why 'done' is returned. Doesn't setTimeout only have two parameters? Why does resolve get 'done'

after timeout is executed?
漂亮男人漂亮男人2794 days ago594

reply all(2)I'll reply

  • 高洛峰

    高洛峰2017-05-16 13:32:47

    https://developer.mozilla.org...

    The ability to pass extra parameters to the delay function

    reply
    0
  • 某草草

    某草草2017-05-16 13:32:47

    setTimeout The first parameter is the callback function, the second is the time to delay the callback, and after the third parameter is the parameter of the callback function. The way you write it is equivalent to

    setTimeout(function () {
        resolve('done')
    }, ms)

    reply
    0
  • Cancelreply