【发布时间】:2018-10-30 17:23:58
【问题描述】:
我正在尝试使用具有 Promise.resolve 格式的 setTimeout 方法。我想我快要得到它了,但我没有在我的控制台中得到我所期望的。
const good = Promise.resolve(setTimeout(() => 'success', 4000));
console.log(good);
//prints '51' instead of 'success'
我认为'51'是成功的数值。如果我是正确的,我想知道如果它在引号中,为什么它会打印它而不是字符串 isteald。
提前感谢您的帮助!
编辑:澄清一下,这是 Udemy 讲师给我的挑战。我在问为什么我的特定代码不起作用而不是答案。
这是挑战:
const success = new Promise((resolve, reject) => {
if (true) {
setTimeout(resolve, 4000, 'success')
} else {
reject('error it broke')
}
});
success
.then (() => console.log('success!'))
3. Read about Promise.resolve() and Promise.reject(). How can you make
the above promise shorter with Promise.resolve() and console log "success"
当我查找这个时,这似乎并不可行,但这是一个挑战,所以我认为我错过了一些东西。
我希望这有助于澄清事情
【问题讨论】:
-
我希望它打印
Promise<51>,实际上。而setTimeout()的返回值是一个句柄,你可以传递给clearTimeout()。 -
当你 console.log 的时候,输出是什么?我希望它返回一个已解决的承诺?
-
我将得到的输出作为注释包含在@EddieDelRio 的代码中。
标签: javascript promise settimeout