Created
April 3, 2018 17:37
-
-
Save nmccready/112f533d1ff55705c9a2a3e366241480 to your computer and use it in GitHub Desktop.
promise pollyfills
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Promise } from 'es6-promise'; | |
import PromiseTry from 'es6-promise-try'; | |
import './async'; | |
if (!Promise.try) { | |
Promise.try = PromiseTry; | |
} | |
if (!Promise.try) { | |
Promise.try = PromiseTry; | |
} | |
function hash(obj) { | |
const promises = []; | |
let index = 0; | |
const indexToKey = {}; | |
for (const key in obj) { | |
indexToKey[index] = key; | |
promises.push(obj[key]); | |
index += 1; | |
} | |
return Promise.all(promises).then((values) => { | |
const obj = {}; | |
values.forEach((v, i) => { | |
obj[indexToKey[i]] = v; | |
}); | |
return obj; | |
}); | |
} | |
if (!Promise.hash) { | |
Promise.hash = hash; | |
} | |
if (!Promise.props) { | |
Promise.props = hash; | |
} | |
function join(...args) { | |
return Promise.all(args).then(values => Promise.resolve(...values)); | |
} | |
if (!Promise.join) { | |
Promise.join = join; | |
} | |
if (typeof global !== 'undefined') { | |
global.Promise = Promise; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment