简体   繁体   English

为什么我不能直接在 SetTimeout 函数中为 Promise 调用 resolve()

[英]Why can I not call resolve() for Promises in SetTimeout Function directly

I have two versions of a sleep function, one is waiting for the resolve the other isn't:我有两个版本的睡眠功能,一个正在等待解决,另一个不是:

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

  function sleephelper2(ms) {
    return new Promise(function(resolve) {
        setTimeout(resolve('done'), ms);           
    })
  }

Then I call either sleephelper1 or sleephelper2:然后我调用 sleephelper1 或 sleephelper2:

async function test(){
var test = await sleephelper1(3000);
console.log(test)
console.log("exit test function")
  }

test()

The first one is waiting 3 seconds before in resolves.第一个在解析前等待 3 秒。 But sleephelper2 ist not working properly.但是 sleephelper2 无法正常工作。 The code gets executed immediatly.代码立即执行。 I thought that SetTimeout can delay the call of a function by a given amount of time.我认为 SetTimeout 可以将函数的调用延迟给定的时间。 Is resolve() not a function ? resolve() 不是函数吗? I have found this post JavaScript promise resolving with setTimeout which is quite what I am asking here, exept that I am using async await.我发现这篇文章使用 setTimeout 解决 JavaScript 承诺,这正是我在这里问的问题,除了我使用的是异步等待。 Also I did not get the explanation.我也没有得到解释。 Could somebody explain to me why this behaves the way it does ?有人可以向我解释为什么会这样吗?

setTimeout(() => resolve('done'), ms);

This means "create a function with the text () => resolve('done') and pass it into setTimeout".这意味着“使用文本() => resolve('done')创建一个函数并将其传递给 setTimeout”。 setTimeout will wait the specified amount of time, and then call that function. setTimeout 将等待指定的时间量,然后调用该函数。

setTimeout(resolve('done'), ms);

This means "immediately call resolve('done') and pass its result into settimeout".这意味着“立即调用resolve('done')并将其结果传递给 settimeout”。 the return value from resolve is undefined , so undefined is passed into setTimeout. resolve 的返回值是undefined ,所以 undefined 被传递到 setTimeout 中。 There is thus no function for setTimeout to run 3 seconds later.因此, setTimeout 没有功能可以在 3 秒后运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在for循环中解析setTimeout函数内部的promise - Resolve promises inside setTimeout function in for-loop 如果我直接调用包含 setTimeout 逻辑的 function,则反应中的去抖动不起作用 - Debouncing in react is not working if I directly call the function containing logic for setTimeout 为什么不能将函数调用(而不是函数引用或匿名函数)传递给setTimeout()? - Why can I not pass a function call (rather than a function reference or an anonymous function) to setTimeout()? 带有 setTimeout 的 JavaScript Promise 按顺序解析 - JavaScript Promises with setTimeout resolve in sequence 是什么决定了使用promises或setTimeout的延迟函数的调用顺序? - What determines the call order of deferred function using promises or setTimeout? 为什么我需要包装器函数以setTimeout递归调用 - Why do I need a wrapper function to recursively call with setTimeout 为什么 setTimeout 中的 promise 在随后的 setTimeout function 之前解决? - Why does a promise inside setTimeout resolve before a subsequent setTimeout function? 我尝试在1秒后(使用setTimeout)调用.hide(),但它直接执行了,为什么? - I try to call .hide() after 1sec (using setTimeout), but it's executed directly, why? 异步 javascript 承诺使用 settimeout 调用 - asynchronous javascript promises call with settimeout 您可以为Promises定义解析功能吗? - Can you define a resolve function for Promises?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM