Created
July 23, 2013 20:31
-
-
Save emcmanus/6065901 to your computer and use it in GitHub Desktop.
Parse.Promise Edge Cases
This file contains hidden or 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
try | |
Parse.Promise.as().then( -> | |
Parse.Promise.as 1, 2, 3 | |
).then((a, b, c) -> | |
# a, b, and c all have values | |
console.error "1. a: #{a}, b: #{b}, c: #{c}" | |
) | |
# Can we transform Parse.Promise.error into a success? | |
Parse.Promise.error().then( null, (error) -> | |
Parse.Promise.as "I'm fine" | |
).then((arg) -> | |
# The success block prints with "I'm fine" | |
console.error "2. Success block called: #{arg}" | |
, (error) -> | |
console.error "2. Failure block called: #{error}" | |
) | |
# If a `then` callback returns an object, does the next then get called | |
# with that object as the argument? | |
Parse.Promise.as().then( -> | |
"I'm not a promise" | |
).then((arg) -> | |
# This block prints with "I'm not a promise" | |
console.error "3. Argument in success block: #{arg}" | |
, (error) -> | |
console.error "3. Argument in failure block: #{error}" | |
) | |
# What if the object is an array? (Does the promise resolve with apply()?) | |
Parse.Promise.as().then( -> | |
["I'm not a promise", "either"] | |
).then((arg1, arg2) -> | |
# arg1 is an array, arg2 is undefined | |
console.error "4. Arguments in success block: #{JSON.stringify(arg1)}, #{JSON.stringify(arg2)}" | |
, (error) -> | |
console.error "4. Argument in failure block: #{error}" | |
) | |
Parse.Promise.as().then( -> | |
Parse.Promise.when( | |
1 | |
2 | |
3 | |
) | |
).then((a, b, c) -> | |
# a, b and c have the values 1, 2 and 3 | |
console.error "5. a: #{a}, b: #{b}, c: #{c}" | |
, (error) -> | |
console.error "5. Error in second promise: #{JSON.stringify(error)}" | |
) | |
catch e | |
console.error "DONE - Caught error in test block: #{JSON.stringify(e)}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment