javascript - 创建一个已解决的 A+ promise

标签 javascript promise

这是一个非常深奥的问题。

编写接受单个值并返回兼容的函数的简单方法是什么 Promises/A+立即用提供的值解决。我想在不使用现有库的情况下执行此操作。

我想我已经弄清楚了,但是规范非常具体,我觉得我可能错过了一些东西。我也很想知道是否有更好的方法。

本质上,以下内容是否满足创建已解决 promise 的 A+/Promises 规范。

var resolvedPromise = (function() {
  function makePromise(isRejected, value) {
    var promise1;

    promise1 = {
      then: function(onFulfilled, onRejected) {
        var callback, promise2;

        promise2 = promise1;

        callback = isRejected ? onRejected : onFulfilled;

        if (typeof callback === 'function') {
          try {
            promise2 = callback(value);
          }
          catch (ex) {
            promise2 = makePromise(true, ex);
          }
        }

        if (promise2 == null || typeof promise2.then !== 'function') {
          promise2 = makePromise(false, promise2);
        }

        return promise2;
      }
    }

    return promise1;
  }

  return makePromise.bind(null, false);
})();

最佳答案

What is a simple way of writing a function which accepts a single value and returns a compliant Promises/A+ which is immediately resolved with the provided value. I want to do this without using an existing library.

没有。基本上,在不使用现有库的情况下创建 Promises/A+ 兼容的 Promise 意味着您必须编写自己的兼容库;这是一项不平凡的任务。

the spec is very specific and I feel like I might have missed something.

Promises/A+ 规范仅涉及 Promise 的 .then 方法应如何工作,以及库应如何处理具有 then 方法但不具有 then 方法的对象自己的 Promise 类型的实例。

该规范没有说明如何或通过什么方法创建 Promise 对象,也没有说明如何解决它。

I think I've figured it out

不,您的 then 方法违反了规范的一些要点;最具体的是#2.2.4:

onFulfilled or onRejected must not be called until the execution context stack contains only platform code.

Here “platform code” means engine, environment, and promise implementation code. In practice, this requirement ensures that onFulfilled and onRejected execute asynchronously, after the event loop turn in which then is called, and with a fresh stack. This can be implemented with either a “macro-task” mechanism such as setTimeout or setImmediate, or with a “micro-task” mechanism such as MutationObserver or process.nextTick. Since the promise implementation is considered platform code, it may itself contain a task-scheduling queue or “trampoline” in which the handlers are called

此外,除了异步执行它们(即使 Promise 已经解决)并仍然立即返回结果的 Promise 带来的主要复杂性之外,您当前的代码不遵循 Promise Resolution em> 详细信息并立即使用 then 方法返回所有对象,即使它们不是一致的 promise 。

I'm also curious to know if there is a better way to do it.

使用现有的库:-)

关于javascript - 创建一个已解决的 A+ promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25821939/

相关文章:

javascript - 如何解决 fetch 中将循环结构转换为 JSON 的问题

javascript - 如何在 JSON 对象中向 JStree 插件的子级添加 href 链接?

javascript - 如何正确解析和使用promise.all()

javascript - Angular 1.2 Iframe ng-src 不更新 pdf

javascript - 如何从 QUnit 测试中触发原生 Javascript 事件?

javascript - jQuery Deferreds - 连续调用 Deferreds 数组

javascript - 如何嵌套 Promise(Promise.all 内的 Promise.all)

javascript - 如何使用 Request 库和 Bluebird 发送 post 请求?

javascript - 从服务器获取脚本位置

javascript - Highcharts - 在向下钻取项上正确切换表格