Skip to content

Instantly share code, notes, and snippets.

@DC3
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save DC3/d2c37399eae2f7f08d16 to your computer and use it in GitHub Desktop.

Select an option

Save DC3/d2c37399eae2f7f08d16 to your computer and use it in GitHub Desktop.
do ->
hasPromise = (window.Promise and window.Promise.all)
hasjQuery = (($ = window.jQuery) and window.jQuery.fn.jquery)
return if hasPromise or not hasjQuery
Promise = (func) ->
$.Deferred(({resolve, reject}) ->
func.call null, resolve, reject
).promise()
Promise.all = (arr) -> new Promise (resolve, reject) ->
if Object::toString.call(arr) isnt '[object Array]'
throw new TypeError 'Promise.all first param must be An iterable object'
handleArguments = (args...) -> resolve(args)
$.when(arr...).then(handleArguments , reject)
Promise.resolve = -> $.Deferred().resolve arguments...
Promise.reject = -> $.Deferred().reject arguments...
# Promise.race = -> # WARN: jQuery do not have race method
window.Promise = Promise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment