(function() {
var define, requireModule, require, requirejs; (function() {
var registry = {},
seen = {}; define = function(name, deps, callback) {
registry[name] = {
deps: deps,
callback: callback
};
}; requirejs = require = requireModule = function(name) {
requirejs._eak_seen = registry; if (seen[name]) {
return seen[name];
}
seen[name] = {}; if (!registry[name]) {
throw new Error("Could not find module " + name);
} var mod = registry[name],
deps = mod.deps,
callback = mod.callback,
reified = [],
exports; for (var i = 0,
l = deps.length; i < l; i++) {
if (deps[i] === 'exports') {
reified.push(exports = {});
} else {
reified.push(requireModule(resolve(deps[i])));
}
} var value = callback.apply(this, reified);
return seen[name] = exports || value; function resolve(child) {
if (child.charAt(0) !== '.') {
return child;
}
var parts = child.split("/");
var parentBase = name.split("/").slice(0, -1); for (var i = 0,
l = parts.length; i < l; i++) {
var part = parts[i]; if (part === '..') {
parentBase.pop();
} else if (part === '.') {
continue;
} else {
parentBase.push(part);
}
} return parentBase.join("/");
}
};
})(); define("promise/all", ["./utils", "exports"],
function(__dependency1__, __exports__) {
"use strict";
/* global toString */ var isArray = __dependency1__.isArray;
var isFunction = __dependency1__.isFunction; /**
Returns a promise that is fulfilled when all the given promises have been
fulfilled, or rejected if any of them become rejected. The return promise
is fulfilled with an array that gives all the values in the order they were
passed in the `promises` array argument. Example: ```javascript
var promise1 = RSVP.resolve(1);
var promise2 = RSVP.resolve(2);
var promise3 = RSVP.resolve(3);
var promises = [ promise1, promise2, promise3 ]; RSVP.all(promises).then(function(array){
// The array here would be [ 1, 2, 3 ];
});
``` If any of the `promises` given to `RSVP.all` are rejected, the first promise
that is rejected will be given as an argument to the returned promises's
rejection handler. For example: Example: ```javascript
var promise1 = RSVP.resolve(1);
var promise2 = RSVP.reject(new Error("2"));
var promise3 = RSVP.reject(new Error("3"));
var promises = [ promise1, promise2, promise3 ]; RSVP.all(promises).then(function(array){
// Code here never runs because there are rejected promises!
}, function(error) {
// error.message === "2"
});
``` @method all
@for RSVP
@param {Array} promises
@param {String} label
@return {Promise} promise that is fulfilled when all `promises` have been
fulfilled, or rejected if any of them become rejected.
*/
function all(promises) {
/*jshint validthis:true */
var Promise = this; if (!isArray(promises)) {
throw new TypeError('You must pass an array to all.');
} return new Promise(function(resolve, reject) {
var results = [],
remaining = promises.length,
promise; if (remaining === 0) {
resolve([]);
} function resolver(index) {
return function(value) {
resolveAll(index, value);
};
} function resolveAll(index, value) {
results[index] = value;
if (--remaining === 0) {
resolve(results);
}
} for (var i = 0; i < promises.length; i++) {
promise = promises[i]; if (promise && isFunction(promise.then)) {
promise.then(resolver(i), reject);
} else {
resolveAll(i, promise);
}
}
});
} __exports__.all = all;
});
define("promise/asap", ["exports"],
function(__exports__) {
"use strict";
var browserGlobal = (typeof window !== 'undefined') ? window: {};
var BrowserMutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver;
var local = (typeof global !== 'undefined') ? global: this; // node
function useNextTick() {
return function() {
process.nextTick(flush);
};
} function useMutationObserver() {
var iterations = 0;
var observer = new BrowserMutationObserver(flush);
var node = document.createTextNode('');
observer.observe(node, {
characterData: true
}); return function() {
node.data = (iterations = ++iterations % 2);
};
} function useSetTimeout() {
return function() {
local.setTimeout(flush, 1);
};
} var queue = [];
function flush() {
for (var i = 0; i < queue.length; i++) {
var tuple = queue[i];
var callback = tuple[0],
arg = tuple[1];
callback(arg);
}
queue = [];
} var scheduleFlush; // Decide what async method to use to triggering processing of queued callbacks:
if (typeof process !== 'undefined' && {}.toString.call(process) === '[object process]') {
scheduleFlush = useNextTick();
} else if (BrowserMutationObserver) {
scheduleFlush = useMutationObserver();
} else {
scheduleFlush = useSetTimeout();
} function asap(callback, arg) {
var length = queue.push([callback, arg]);
if (length === 1) {
// If length is 1, that means that we need to schedule an async flush.
// If additional callbacks are queued before the queue is flushed, they
// will be processed by this flush that we are scheduling.
scheduleFlush();
}
} __exports__.asap = asap;
});
define("promise/cast", ["exports"],
function(__exports__) {
"use strict";
/**
`RSVP.Promise.cast` returns the same promise if that promise shares a constructor
with the promise being casted. Example: ```javascript
var promise = RSVP.resolve(1);
var casted = RSVP.Promise.cast(promise); console.log(promise === casted); // true
``` In the case of a promise whose constructor does not match, it is assimilated.
The resulting promise will fulfill or reject based on the outcome of the
promise being casted. In the case of a non-promise, a promise which will fulfill with that value is
returned. Example: ```javascript
var value = 1; // could be a number, boolean, string, undefined...
var casted = RSVP.Promise.cast(value); console.log(value === casted); // false
console.log(casted instanceof RSVP.Promise) // true casted.then(function(val) {
val === value // => true
});
``` `RSVP.Promise.cast` is similar to `RSVP.resolve`, but `RSVP.Promise.cast` differs in the
following ways:
* `RSVP.Promise.cast` serves as a memory-efficient way of getting a promise, when you
have something that could either be a promise or a value. RSVP.resolve
will have the same effect but will create a new promise wrapper if the
argument is a promise.
* `RSVP.Promise.cast` is a way of casting incoming thenables or promise subclasses to
promises of the exact class specified, so that the resulting object's `then` is
ensured to have the behavior of the constructor you are calling cast on (i.e., RSVP.Promise). @method cast
@for RSVP
@param {Object} object to be casted
@return {Promise} promise that is fulfilled when all properties of `promises`
have been fulfilled, or rejected if any of them become rejected.
*/ function cast(object) {
/*jshint validthis:true */
if (object && typeof object === 'object' && object.constructor === this) {
return object;
} var Promise = this; return new Promise(function(resolve) {
resolve(object);
});
} __exports__.cast = cast;
});
define("promise/config", ["exports"],
function(__exports__) {
"use strict";
var config = {
instrument: false
}; function configure(name, value) {
if (arguments.length === 2) {
config[name] = value;
} else {
return config[name];
}
} __exports__.config = config;
__exports__.configure = configure;
});
define("promise/polyfill", ["./promise", "./utils", "exports"],
function(__dependency1__, __dependency2__, __exports__) {
"use strict";
var RSVPPromise = __dependency1__.Promise;
var isFunction = __dependency2__.isFunction; function polyfill() {
var es6PromiseSupport = "Promise" in window &&
// Some of these methods are missing from
// Firefox/Chrome experimental implementations
"cast" in window.Promise && "resolve" in window.Promise && "reject" in window.Promise && "all" in window.Promise && "race" in window.Promise &&
// Older version of the spec had a resolver object
// as the arg rather than a function
(function() {
var resolve;
new window.Promise(function(r) {
resolve = r;
});
return isFunction(resolve);
} ()); if (!es6PromiseSupport) {
window.Promise = RSVPPromise;
}
} __exports__.polyfill = polyfill;
});
define("promise/promise", ["./config", "./utils", "./cast", "./all", "./race", "./resolve", "./reject", "./asap", "exports"],
function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __dependency5__, __dependency6__, __dependency7__, __dependency8__, __exports__) {
"use strict";
var config = __dependency1__.config;
var configure = __dependency1__.configure;
var objectOrFunction = __dependency2__.objectOrFunction;
var isFunction = __dependency2__.isFunction;
var now = __dependency2__.now;
var cast = __dependency3__.cast;
var all = __dependency4__.all;
var race = __dependency5__.race;
var staticResolve = __dependency6__.resolve;
var staticReject = __dependency7__.reject;
var asap = __dependency8__.asap; var counter = 0; config.async = asap; // default async is asap;
function Promise(resolver) {
if (!isFunction(resolver)) {
throw new TypeError('You must pass a resolver function as the first argument to the promise constructor');
} if (! (this instanceof Promise)) {
throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.");
} this._subscribers = []; invokeResolver(resolver, this);
} function invokeResolver(resolver, promise) {
function resolvePromise(value) {
resolve(promise, value);
} function rejectPromise(reason) {
reject(promise, reason);
} try {
resolver(resolvePromise, rejectPromise);
} catch(e) {
rejectPromise(e);
}
} function invokeCallback(settled, promise, callback, detail) {
var hasCallback = isFunction(callback),
value,
error,
succeeded,
failed; if (hasCallback) {
try {
value = callback(detail);
succeeded = true;
} catch(e) {
failed = true;
error = e;
}
} else {
value = detail;
succeeded = true;
} if (handleThenable(promise, value)) {
return;
} else if (hasCallback && succeeded) {
resolve(promise, value);
} else if (failed) {
reject(promise, error);
} else if (settled === FULFILLED) {
resolve(promise, value);
} else if (settled === REJECTED) {
reject(promise, value);
}
} var PENDING = void 0;
var SEALED = 0;
var FULFILLED = 1;
var REJECTED = 2; function subscribe(parent, child, onFulfillment, onRejection) {
var subscribers = parent._subscribers;
var length = subscribers.length; subscribers[length] = child;
subscribers[length + FULFILLED] = onFulfillment;
subscribers[length + REJECTED] = onRejection;
} function publish(promise, settled) {
var child, callback, subscribers = promise._subscribers,
detail = promise._detail; for (var i = 0; i < subscribers.length; i += 3) {
child = subscribers[i];
callback = subscribers[i + settled]; invokeCallback(settled, child, callback, detail);
} promise._subscribers = null;
} Promise.prototype = {
constructor: Promise, _state: undefined,
_detail: undefined,
_subscribers: undefined, then: function(onFulfillment, onRejection) {
var promise = this; var thenPromise = new this.constructor(function() {}); if (this._state) {
var callbacks = arguments;
config.async(function invokePromiseCallback() {
invokeCallback(promise._state, thenPromise, callbacks[promise._state - 1], promise._detail);
});
} else {
subscribe(this, thenPromise, onFulfillment, onRejection);
} return thenPromise;
}, 'catch': function(onRejection) {
return this.then(null, onRejection);
}
}; Promise.all = all;
Promise.cast = cast;
Promise.race = race;
Promise.resolve = staticResolve;
Promise.reject = staticReject; function handleThenable(promise, value) {
var then = null,
resolved; try {
if (promise === value) {
throw new TypeError("A promises callback cannot return that same promise.");
} if (objectOrFunction(value)) {
then = value.then; if (isFunction(then)) {
then.call(value,
function(val) {
if (resolved) {
return true;
}
resolved = true; if (value !== val) {
resolve(promise, val);
} else {
fulfill(promise, val);
}
},
function(val) {
if (resolved) {
return true;
}
resolved = true; reject(promise, val);
}); return true;
}
}
} catch(error) {
if (resolved) {
return true;
}
reject(promise, error);
return true;
} return false;
} function resolve(promise, value) {
if (promise === value) {
fulfill(promise, value);
} else if (!handleThenable(promise, value)) {
fulfill(promise, value);
}
} function fulfill(promise, value) {
if (promise._state !== PENDING) {
return;
}
promise._state = SEALED;
promise._detail = value; config.async(publishFulfillment, promise);
} function reject(promise, reason) {
if (promise._state !== PENDING) {
return;
}
promise._state = SEALED;
promise._detail = reason; config.async(publishRejection, promise);
} function publishFulfillment(promise) {
publish(promise, promise._state = FULFILLED);
} function publishRejection(promise) {
publish(promise, promise._state = REJECTED);
} __exports__.Promise = Promise;
});
define("promise/race", ["./utils", "exports"],
function(__dependency1__, __exports__) {
"use strict";
/* global toString */
var isArray = __dependency1__.isArray; /**
`RSVP.race` allows you to watch a series of promises and act as soon as the
first promise given to the `promises` argument fulfills or rejects. Example: ```javascript
var promise1 = new RSVP.Promise(function(resolve, reject){
setTimeout(function(){
resolve("promise 1");
}, 200);
}); var promise2 = new RSVP.Promise(function(resolve, reject){
setTimeout(function(){
resolve("promise 2");
}, 100);
}); RSVP.race([promise1, promise2]).then(function(result){
// result === "promise 2" because it was resolved before promise1
// was resolved.
});
``` `RSVP.race` is deterministic in that only the state of the first completed
promise matters. For example, even if other promises given to the `promises`
array argument are resolved, but the first completed promise has become
rejected before the other promises became fulfilled, the returned promise
will become rejected: ```javascript
var promise1 = new RSVP.Promise(function(resolve, reject){
setTimeout(function(){
resolve("promise 1");
}, 200);
}); var promise2 = new RSVP.Promise(function(resolve, reject){
setTimeout(function(){
reject(new Error("promise 2"));
}, 100);
}); RSVP.race([promise1, promise2]).then(function(result){
// Code here never runs because there are rejected promises!
}, function(reason){
// reason.message === "promise2" because promise 2 became rejected before
// promise 1 became fulfilled
});
``` @method race
@for RSVP
@param {Array} promises array of promises to observe
@param {String} label optional string for describing the promise returned.
Useful for tooling.
@return {Promise} a promise that becomes fulfilled with the value the first
completed promises is resolved with if the first completed promise was
fulfilled, or rejected with the reason that the first completed promise
was rejected with.
*/
function race(promises) {
/*jshint validthis:true */
var Promise = this; if (!isArray(promises)) {
throw new TypeError('You must pass an array to race.');
}
return new Promise(function(resolve, reject) {
var results = [],
promise; for (var i = 0; i < promises.length; i++) {
promise = promises[i]; if (promise && typeof promise.then === 'function') {
promise.then(resolve, reject);
} else {
resolve(promise);
}
}
});
} __exports__.race = race;
});
define("promise/reject", ["exports"],
function(__exports__) {
"use strict";
/**
`RSVP.reject` returns a promise that will become rejected with the passed
`reason`. `RSVP.reject` is essentially shorthand for the following: ```javascript
var promise = new RSVP.Promise(function(resolve, reject){
reject(new Error('WHOOPS'));
}); promise.then(function(value){
// Code here doesn't run because the promise is rejected!
}, function(reason){
// reason.message === 'WHOOPS'
});
``` Instead of writing the above, your code now simply becomes the following: ```javascript
var promise = RSVP.reject(new Error('WHOOPS')); promise.then(function(value){
// Code here doesn't run because the promise is rejected!
}, function(reason){
// reason.message === 'WHOOPS'
});
``` @method reject
@for RSVP
@param {Any} reason value that the returned promise will be rejected with.
@param {String} label optional string for identifying the returned promise.
Useful for tooling.
@return {Promise} a promise that will become rejected with the given
`reason`.
*/
function reject(reason) {
/*jshint validthis:true */
var Promise = this; return new Promise(function(resolve, reject) {
reject(reason);
});
} __exports__.reject = reject;
});
define("promise/resolve", ["exports"],
function(__exports__) {
"use strict";
/**
`RSVP.resolve` returns a promise that will become fulfilled with the passed
`value`. `RSVP.resolve` is essentially shorthand for the following: ```javascript
var promise = new RSVP.Promise(function(resolve, reject){
resolve(1);
}); promise.then(function(value){
// value === 1
});
``` Instead of writing the above, your code now simply becomes the following: ```javascript
var promise = RSVP.resolve(1); promise.then(function(value){
// value === 1
});
``` @method resolve
@for RSVP
@param {Any} value value that the returned promise will be resolved with
@param {String} label optional string for identifying the returned promise.
Useful for tooling.
@return {Promise} a promise that will become fulfilled with the given
`value`
*/
function resolve(value) {
/*jshint validthis:true */
var Promise = this;
return new Promise(function(resolve, reject) {
resolve(value);
});
} __exports__.resolve = resolve;
});
define("promise/utils", ["exports"],
function(__exports__) {
"use strict";
function objectOrFunction(x) {
return isFunction(x) || (typeof x === "object" && x !== null);
} function isFunction(x) {
return typeof x === "function";
} function isArray(x) {
return Object.prototype.toString.call(x) === "[object Array]";
} // Date.now is not available in browsers < IE9
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now#Compatibility
var now = Date.now ||
function() {
return new Date().getTime();
}; __exports__.objectOrFunction = objectOrFunction;
__exports__.isFunction = isFunction;
__exports__.isArray = isArray;
__exports__.now = now;
});
requireModule('promise/polyfill').polyfill();
} ());

下载地址:

http://s3.amazonaws.com/es6-promises/promise-0.1.1.js

http://s3.amazonaws.com/es6-promises/promise-0.1.1.min.js

PromiseJs的更多相关文章

  1. Promise-js中的同步和异步

    js中的同步和异步   自从读了研后,走上了学术之路,每天除了看论文就是做实验,最后发现自己还是喜欢开发呀,于是我又重回前端啦~ 隔了这么久没学前端,好像很多东西都忘了不少,而且不得不说前端的技术更新 ...

  2. Promise-js异步加载解决方案

    范例: var p = new Promise(function(resolve, reject){ //做一些异步操作 setTimeout(function(){ console.log('执行完 ...

  3. Promise deeply lookup

    Motivation Consider the following synchronous JavaScript function to read a file and parse it as JSO ...

  4. Javascript Promise入门

    是什么? https://www.promisejs.org/ What is a promise? The core idea behind promises is that a promise r ...

  5. JS魔法堂:剖析源码理解Promises/A规范

    一.前言 Promises/A是由CommonJS组织制定的异步模式编程规范,有不少库已根据该规范及后来经改进的Promises/A+规范提供了实现 如Q, Bluebird, when, rsvp. ...

  6. 一个简单的Promise 实现

    用了这么长时间的promise,也看了很多关于promise 的文章博客,对promise 算是些了解.但是要更深的理解promise,最好的办法还是自己实现一个. 我大概清楚promise 是对异步 ...

  7. (翻译)异步编程之Promise(1):初见魅力

    原文:https://www.promisejs.org/ by Forbes Lindesay 异步编程系列教程: (翻译)异步编程之Promise(1)--初见魅力 异步编程之Promise(2) ...

  8. 【JavaScript】JavaScript Promise 探微

    http://www.html-js.com/article/Promise-translation-JavaScript-Promise-devil-details 原文链接:JavaScript ...

  9. 实现nodejs的promises库(基于promise.js改写)

    原promise.js库地址:https://github.com/stackp/promisejs promises是JavaScript实现优雅编程的一个非常不错的轻量级框架.该框架可以让你从杂乱 ...

随机推荐

  1. 请听一个故事------>你真的认为iPhone只是一部手机?苹果惊天秘密!!

    在网上看到的一篇小说,感觉有点意思,转载过来大家一起围观下,作者很幽默很风趣. 导读:iPhone的隐藏功能!Jobs的军方身份!图灵服毒自杀的传奇故事!中兴华为的神秘背景! 你真的认为iPhone只 ...

  2. 运行 Tomcat, 在 Intellij IDEA 控制台输出中文乱码的解决方法

    打开 Run/Debug Configurations → Tomcat Server → 要运行的 Tomcat → Server 页签,在 VM options 中输入: -Dfile.encod ...

  3. asp.net 站点公布

    asp.net 站点公布 1.安装并打开IIS. 2.用visual studio创建站点,文件夹为d:\website\website1 3.IIS中加入站点,站点名称必填.物理路径与已有的相应好. ...

  4. 024-Spring Boot 应用的打包和部署

    一.概述 二.手工打包[不推荐] 打包命令:maven clean package 打包并导出依赖:maven clean package dependency:copy-dependencies 1 ...

  5. Commit 函数WAIT = 'X'.

    BAPI_TRANSACTION_COMMIT IF WAIT EQ SPACE. COMMIT WORK. ELSE. COMMIT WORK AND WAIT. IF SY-SUBRC NE . ...

  6. python并发编程之多线程2---(死锁与递归锁,信号量等)

    一.死锁现象与递归锁 进程也是有死锁的 所谓死锁: 是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用, 它们都将无法推进下去.此时称系统处于死锁状态或系统 ...

  7. MyEclipse工具栏的隐藏与显示及自定义

    Myeclipse的工具栏 1.隐藏 工具栏---->右键---->hide toolbar 2.显示     window ----> show toolbar 3.自定义     ...

  8. 前端基础之Bootstrap介绍

    bootstrap简介 http://v3.bootcss.com/ Bootstrap优点:  下载:  Bootstrap引入 <meta name="viewport" ...

  9. JQuery Div层滚动条控制(模拟横向滚动条在最顶端显示)

    想让DIV层滚动条显示在顶端,CSS样式没找到相关属性,于是用2个DIV层来模拟做了一个.经测试IE浏览器上显示并不太美观!不知道是否还有更好的办法可以实现这功能呢?   aaaaaaasssssss ...

  10. Qt浅谈之二十六图片滑动效果

    一.简介 博客中发现有作者写的仿360的代码,觉得其中图片滑动的效果很有意思,特提取其中的代码.并加上类似mac的画面移动的动画效果. 二.详解 1.代码一:界面滑动(QWidget) (1)slid ...