admin管理员组文章数量:1389750
I don't understand why the first setTimeout function works but the second one doesn't. The first one is mented out when I run the second setTimeout. But instead of resolving after 3 seconds it resolves immediately.
I'm new to the whole 'promise' thing and the tutorial I'm working through uses promises with setTimeout a lot.
let promise = new Promise( ( resolve, reject ) => {
/* why does setTimeout work with this one... */
setTimeout( () => resolve( 'Job\'s done!!!' ), 3000 );
/* but not with this one */
setTimeout( resolve('done'), 3000 );
} );
promise.then(
result => alert( result )
);
I don't understand why the first setTimeout function works but the second one doesn't. The first one is mented out when I run the second setTimeout. But instead of resolving after 3 seconds it resolves immediately.
I'm new to the whole 'promise' thing and the tutorial I'm working through uses promises with setTimeout a lot.
let promise = new Promise( ( resolve, reject ) => {
/* why does setTimeout work with this one... */
setTimeout( () => resolve( 'Job\'s done!!!' ), 3000 );
/* but not with this one */
setTimeout( resolve('done'), 3000 );
} );
promise.then(
result => alert( result )
);
Share
Improve this question
asked Oct 3, 2017 at 19:12
IvanIvan
1432 silver badges12 bronze badges
1
- 2 because second one calls the method and returns what it returns to be called by setTimeout.... – epascarello Commented Oct 3, 2017 at 19:14
1 Answer
Reset to default 5/* 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!!!' );
}
本文标签: JavaScript promise resolving with setTimeoutStack Overflow
版权声明:本文标题:JavaScript promise resolving with setTimeout - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744686199a2619715.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论