JavaScript promise resolving with setTimeout

前端 未结 1 514
臣服心动
臣服心动 2021-01-15 00:35

I don\'t understand why the first setTimeout function works but the second one doesn\'t. The first one is commented out when I run the second setTimeout. But instead of reso

相关标签:
1条回答
  • 2021-01-15 01:04
    /* why does setTimeout work with this one... */
     setTimeout( () => resolve( 'Job\'s done!!!' ), 3000 );
    

    when the timeout occur you call a function () => ... wich when executed till resolve the promise

    /* but not with this one */
     setTimeout( resolve('done'), 3000 );
    

    here you actually resolve the promise (you execute the result function) and pass the result to the setTimeout function.

    Writing

    () => resolve( 'Job\'s done!!!' )
    

    is the same as

    function() {
        resolve( 'Job\'s done!!!' );
    }
    
    0 讨论(0)
提交回复
热议问题