Sha256: c3db921b6cf5a6331881f16f0f148cad54fb2697835ebe32d76f510e3485fb1e

Contents?: true

Size: 1.97 KB

Versions: 36

Compression:

Stored size: 1.97 KB

Contents

import AwaitValue from "./AwaitValue";
export default function AsyncGenerator(gen) {
  var front, back;

  function send(key, arg) {
    return new Promise(function (resolve, reject) {
      var request = {
        key: key,
        arg: arg,
        resolve: resolve,
        reject: reject,
        next: null
      };

      if (back) {
        back = back.next = request;
      } else {
        front = back = request;
        resume(key, arg);
      }
    });
  }

  function resume(key, arg) {
    try {
      var result = gen[key](arg);
      var value = result.value;
      var wrappedAwait = value instanceof AwaitValue;
      Promise.resolve(wrappedAwait ? value.wrapped : value).then(function (arg) {
        if (wrappedAwait) {
          resume(key === "return" ? "return" : "next", arg);
          return;
        }

        settle(result.done ? "return" : "normal", arg);
      }, function (err) {
        resume("throw", err);
      });
    } catch (err) {
      settle("throw", err);
    }
  }

  function settle(type, value) {
    switch (type) {
      case "return":
        front.resolve({
          value: value,
          done: true
        });
        break;

      case "throw":
        front.reject(value);
        break;

      default:
        front.resolve({
          value: value,
          done: false
        });
        break;
    }

    front = front.next;

    if (front) {
      resume(front.key, front.arg);
    } else {
      back = null;
    }
  }

  this._invoke = send;

  if (typeof gen["return"] !== "function") {
    this["return"] = undefined;
  }
}

if (typeof Symbol === "function" && Symbol.asyncIterator) {
  AsyncGenerator.prototype[Symbol.asyncIterator] = function () {
    return this;
  };
}

AsyncGenerator.prototype.next = function (arg) {
  return this._invoke("next", arg);
};

AsyncGenerator.prototype["throw"] = function (arg) {
  return this._invoke("throw", arg);
};

AsyncGenerator.prototype["return"] = function (arg) {
  return this._invoke("return", arg);
};

Version data entries

36 entries across 36 versions & 5 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/@babel/runtime/helpers/esm/AsyncGenerator.js
trusty-cms-5.0.7 node_modules/@babel/runtime/helpers/esm/AsyncGenerator.js
trusty-cms-5.0.6 node_modules/@babel/runtime/helpers/esm/AsyncGenerator.js
trusty-cms-5.0.5 node_modules/@babel/runtime/helpers/esm/AsyncGenerator.js
trusty-cms-5.0.4 node_modules/@babel/runtime/helpers/esm/AsyncGenerator.js
trusty-cms-5.0.3 node_modules/@babel/runtime/helpers/esm/AsyncGenerator.js
trusty-cms-5.0.2 node_modules/@babel/runtime/helpers/esm/AsyncGenerator.js
trusty-cms-5.0.1 node_modules/@babel/runtime/helpers/esm/AsyncGenerator.js
trusty-cms-4.3.5 node_modules/@babel/runtime/helpers/esm/AsyncGenerator.js
trusty-cms-5.0.0 node_modules/@babel/runtime/helpers/esm/AsyncGenerator.js
trusty-cms-4.3.4 node_modules/@babel/runtime/helpers/esm/AsyncGenerator.js
trusty-cms-4.3.3 node_modules/@babel/runtime/helpers/esm/AsyncGenerator.js
trusty-cms-4.3.2 node_modules/@babel/runtime/helpers/esm/AsyncGenerator.js
trusty-cms-4.3.1 node_modules/@babel/runtime/helpers/esm/AsyncGenerator.js
trusty-cms-4.3 node_modules/@babel/runtime/helpers/esm/AsyncGenerator.js
trusty-cms-4.2.3 node_modules/@babel/runtime/helpers/esm/AsyncGenerator.js
tang-0.2.1 spec/tang_app/node_modules/@babel/runtime/helpers/esm/AsyncGenerator.js
trusty-cms-4.2.2 node_modules/@babel/runtime/helpers/esm/AsyncGenerator.js
trusty-cms-4.2.1 node_modules/@babel/runtime/helpers/esm/AsyncGenerator.js
trusty-cms-4.2 node_modules/@babel/runtime/helpers/esm/AsyncGenerator.js