Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function makePromisesTraceable() {
- var NativePromise = window.Promise;
- window.promiseRegistry = [];
- window.Promise = function(cb) {
- var state = {};
- var p = new NativePromise(function(resolve, reject) {
- var res = function(v) { state.status = 'resolved'; state.value = v; resolve(v); };
- var rej = function(v) { state.status = 'rejected'; state.value = v; reject(v); };
- return cb(res, rej);
- });
- p.callback = cb;
- p.state = state;
- window.promiseRegistry.push(p);
- return p;
- };
- Promise.resolve = function(item) {
- return new Promise(function(resolve) { resolve(item); });
- };
- Promise.all = function(arr) {
- return new Promise(function(resolve) {
- var results = [];
- if (arr.length === 0) resolve(results);
- var pendingCount = arr.length;
- arr.forEach(function(val, index) {
- Promise.resolve(val).then(function(res) {
- results[index] = res;
- pendingCount--;
- if (pendingCount === 0) resolve(results);
- });
- });
- });
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement