我发现原生 ES6 Promise.resolve()
可以作为静态方法直接调用。 Facebook 在他们的 Flux 示例中就是这样使用它的。
但在什么情况下我应该这样做呢?排队买东西?或者不使用 window.setTimeout()
?
最佳答案
当您需要创建已解决的 Promise 时,您应该调用 Promise.resolve(object)
。例如,您可能有一个函数开始从服务器下载资源或返回其缓存版本:
function getImage(imageUrl) {
if (imageUrl in this.cache) {
// no need to download, return immediately
return Promise.resolve(this.cache[imageUrl]);
} else {
return new Promise(function(resolve, reject) {
// start downloading and eventually resolve
});
}
}
关于ecmascript-6 - 我什么时候应该直接调用 Promise.resolve() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32461170/