Created
July 31, 2014 13:00
-
-
Save slorber/b395f7d7225c1b6d198d to your computer and use it in GitHub Desktop.
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
function setupAsyncValueSwapping(atom,path,asyncValue,promise) { | |
var deferred = Q.defer(); | |
promise | |
.then(function asyncCompletionSuccess(data) { | |
var swapped = atom.compareAndSwapPathValue(path,asyncValue,asyncValue.toSuccess(data)); | |
if ( swapped ) { | |
deferred.resolve(data); | |
} else { | |
deferred.reject(new Error("Async value completion for path "+path+" but swap=false")); | |
} | |
}) | |
.fail(function asyncCompletionError(error) { | |
var swapped = atom.compareAndSwapPathValue(path,asyncValue,asyncValue.toError(error)); | |
console.error("Async value completion error",path,"Swap success=",swapped); | |
console.error(error.stack); | |
deferred.reject(error); | |
}) | |
.done(); | |
return deferred.promise; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment