use promise polyfill
This commit is contained in:
parent
b2b0ab34dd
commit
9a396c5173
2 changed files with 96 additions and 95 deletions
1
index.js
1
index.js
|
|
@ -1,5 +1,6 @@
|
|||
"use strict"
|
||||
|
||||
var Promise = require("./promise/promise")
|
||||
var m = require("./render/hyperscript")
|
||||
var renderService = require("./render/render")(window)
|
||||
var redrawService = require("./api/pubsub")()
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
"use strict"
|
||||
{
|
||||
function Promise(executor) {
|
||||
|
||||
if (typeof Promise === "undefined") {
|
||||
var Promise = function(executor) {
|
||||
if (!(this instanceof Promise)) throw new Error("Promise must be called with `new`")
|
||||
if (typeof executor !== "function") throw new Error("executor must be a function")
|
||||
if (typeof executor !== "function") throw new TypeError("executor must be a function")
|
||||
|
||||
var self = this, resolvers = [], rejectors = [], resolveCurrent = handler(resolvers, true), rejectCurrent = handler(rejectors, false)
|
||||
var instance = self._instance = {resolvers: resolvers, rejectors: rejectors}
|
||||
|
|
@ -43,8 +44,8 @@ function Promise(executor) {
|
|||
}
|
||||
|
||||
executeOnce(executor)
|
||||
}
|
||||
Promise.prototype.then = function(onFulfilled, onRejection) {
|
||||
}
|
||||
Promise.prototype.then = function(onFulfilled, onRejection) {
|
||||
var self = this, instance = self._instance
|
||||
function handle(callback, list, next, state) {
|
||||
list.push(function(value) {
|
||||
|
|
@ -57,18 +58,18 @@ Promise.prototype.then = function(onFulfilled, onRejection) {
|
|||
var promise = new Promise(function(resolve, reject) {resolveNext = resolve, rejectNext = reject})
|
||||
handle(onFulfilled, instance.resolvers, resolveNext, true), handle(onRejection, instance.rejectors, rejectNext, false)
|
||||
return promise
|
||||
}
|
||||
Promise.prototype.catch = function(onRejection) {
|
||||
}
|
||||
Promise.prototype.catch = function(onRejection) {
|
||||
return this.then(null, onRejection)
|
||||
}
|
||||
Promise.resolve = function(value) {
|
||||
}
|
||||
Promise.resolve = function(value) {
|
||||
if (value instanceof Promise) return value
|
||||
return new Promise(function(resolve, reject) {resolve(value)})
|
||||
}
|
||||
Promise.reject = function(value) {
|
||||
}
|
||||
Promise.reject = function(value) {
|
||||
return new Promise(function(resolve, reject) {reject(value)})
|
||||
}
|
||||
Promise.all = function(list) {
|
||||
}
|
||||
Promise.all = function(list) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
var total = list.length, count = 0, values = []
|
||||
for (var i = 0; i < list.length; i++) {
|
||||
|
|
@ -85,14 +86,13 @@ Promise.all = function(list) {
|
|||
}(i)
|
||||
}
|
||||
})
|
||||
}
|
||||
Promise.race = function(list) {
|
||||
}
|
||||
Promise.race = function(list) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
for (var i = 0; i < list.length; i++) {
|
||||
list[i].then(resolve, reject)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Promise
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue