本文介绍了Promise.defer()浏览器支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来创建延迟对象,这将在当前范围之外解决。我喜欢延迟对象,因为我在Chrome 38中看到 Promise.defer()会返回延迟对象。

I am looking for a way to create deferred object which will be resolved outside the current scope. I like deferred objects and as I see Promise.defer() in Chrome 38 returns the deferred object.

在最新的Firefox 34 Promise.defer 在Safari 8.0中未定义。

But in latest Firefox 34 Promise.defer is undefined as well in Safari 8.0.

code> Promise.defer 到处都是。未来的地位怎么样?

So I can't use Promise.defer everywhere now. What about future status? Will it be implemented in other browsers or will be removed as deprecated?

推荐答案

根据, .defer 方法是过时的。如果您查看,则表示 Promise.defer 是非标准的,因此不太可能返回。

According to the MDN article on Deferred, the .defer method is obsolete. If you look at this bug issue, it says that Promise.defer is non-standard, so it's not likely to return.

如何重写 Promise.defer 代码,改为使用 new Promise

They offer an example of how to rewrite Promise.defer code, to instead use new Promise.

var deferred = Promise.defer();
doSomething(function cb(good) {
    if (good)
        deferred.resolve();
    else
        deferred.reject();
});
return deferred.promise;



new Promise



new Promise

return new Promise(function(resolve, reject) {
    doSomething(function cb(good) {
        if (good)
            resolve();
        else
            reject();
    });
});

这篇关于Promise.defer()浏览器支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 21:43